java method synchronization and read/write mutual exclusion

女生的网名这么多〃 提交于 2020-01-11 17:39:10

问题


I have two methods read() and write() as below in a class.

class Store
{

  public void write()
  {
    // write to store;
  }

  public string  read()
  {
    // read from store;
  }
}

1) The Store object is a singleton.

2) I have a Writer class which will write to the store and several Reader classes which will read from the store at the same time.

My requirement is that when the writer is writing to the store, all the readers should wait. i.e., when control is in write(), all the calls to read() should be blocked. How do I achieve this? I have tried synchronize(Store.class) in the write() method, but doesn't seem like work for me.


回答1:


The best option in this case is to use a reader-writer lock: ReadWriteLock. It allows a single writer, but multiple concurrent readers, so it's the most efficient mechanism for this type of scenario.

Some sample code:

class Store
{
    private ReadWriteLock rwlock = new ReentrantReadWriteLock();

    public void write()
    {
       rwlock.writeLock().lock();
       try {
          write to store;
       } finally {
          rwlock.writeLock().unlock();
       }
    }

    public String read()
    {
       rwlock.readLock().lock();
       try {
          read from store;
       } finally {
          rwlock.readLock().unlock();
       }
    }
}



回答2:


synchronized is the simplest solution so you should explain what you mean by "doesn't seem like work for me" and perhaps show us how you used it.

The better solution is to use ReentrantReadWriteLock because it allows concurrent access to readers as well.




回答3:


When a method is synchronized on some object, a thread executing this method blocks all the other threads that try to enter another block/method that is synchronized on the same object.

So, if you want the writer threads to forbid any reader thread to read, you must synchronize both the write and the read method. There is no reason to synchronize on the Store class. Synchronizing on the Store object is more natural:

public synchronized void write() {
  write to store;
}

public synchronized String read() {
  read from store;
}

This, however, (maybe) has a drawback: it also forbids two reader threads to read at the same time. If you really need this to happen, you should use a ReadWriteLock. But this will lead to code that is less performant, and harder to understand and maintain. I would only use it if I have measured that this is needed.




回答4:


Use a ReadWriteLock from java.util.concurrent package




回答5:


Take care to read this article:

https://blog.takipi.com/java-8-stampedlocks-vs-readwritelocks-and-synchronized/

  • RWLocks can be slow especially when there are many readers to writers
  • synchronized is on average the most reliably fast
  • there are new locks like StampedLock


来源:https://stackoverflow.com/questions/10372668/java-method-synchronization-and-read-write-mutual-exclusion

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!