简体   繁体   English

java方法同步与读写互斥

[英]java method synchronization and read/write mutual exclusion

I have two methods read() and write() as below in a class.我在一个类中有两个方法read()write()如下。

class Store
{

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

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

1) The Store object is a singleton. 1) Store对象是一个单例。

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. 2)我有一个Writer类将写入商店和几个Reader类将同时从商店读取。

My requirement is that when the writer is writing to the store, all the readers should wait.我的要求是,当作者向商店写信时,所有读者都应该等待。 ie, when control is in write() , all the calls to read() should be blocked.即,当控制在write() ,所有对read()的调用都应该被阻止。 How do I achieve this?我如何实现这一目标? I have tried synchronize(Store.class) in the write() method, but doesn't seem like work for me.我在write()方法中尝试过synchronize(Store.class) ,但似乎对我不起作用。

The best option in this case is to use a reader-writer lock: ReadWriteLock .在这种情况下,最好的选择是使用读写锁: 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();
       }
    }
}

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. synchronized是最简单的解决方案,因此您应该解释“对我来说似乎不起作用”的意思,并可能向我们展示您如何使用它。

The better solution is to use ReentrantReadWriteLock because it allows concurrent access to readers as well.更好的解决方案是使用ReentrantReadWriteLock,因为它也允许并发访问读者。

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.没有理由在Store类上进行同步。 Synchronizing on the Store object is more natural:Store对象上同步更自然:

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 .如果你真的需要这种情况发生,你应该使用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.如果我已经测量到需要这样做,我只会使用它。

使用 java.util.concurrent 包中的 ReadWriteLock

Take care to read this article:请注意阅读这篇文章:

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

  • RWLocks can be slow especially when there are many readers to writers RWLocks 可能很慢,尤其是当有很多读者到作者时
  • synchronized is on average the most reliably fast同步平均是最可靠的快速
  • there are new locks like StampedLock有像 StampedLock 这样的新锁

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM