Java ReentrantReadWriteLock:ReadLock 和 WriteLock 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18354339/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 00:49:39  来源:igfitidea点击:

ReentrantReadWriteLock: what's the difference between ReadLock and WriteLock?

javamultithreadinglockingdeadlockreentrantreadwritelock

提问by u2455061

What I know is:

我所知道的是:

  • ReadLockand WriteLockaffect each other somehow
  • WriteLockis just like synchronized
  • ReadLockseems cannot work alone
  • ReadLockWriteLock以某种方式相互影响
  • WriteLock就像同步
  • ReadLock似乎不能单独工作

采纳答案by OldCurmudgeon

readLock.lock();

读锁.lock();

  • This means that if any other thread is writing(i.e. holds a write lock) then stop here until no other thread is writing.
  • Once the lock is granted no other thread will be allowed to write(i.e. take a write lock) until the lock is released.
  • 这意味着如果任何其他线程正在写入(即持有写锁),则在此处停止,直到没有其他线程正在写入。
  • 一旦锁定被授予 ,在锁定被释放之前将不允许其他线程写入(即获取写入锁定)。


writeLock.lock();

writeLock.lock();

  • This means that if any other thread is reading orwriting, stop here and wait until no other thread is reading or writing.
  • Once the lock is granted, no other thread will be allowed to read orwrite(i.e. take a read or write lock) until the lock is released.
  • 这意味着如果任何其他线程正在读取写入,请在此处停止并等待直到没有其他线程正在读取或写入。
  • 一旦锁定被授予,在锁定被释放之前,将不允许其他线程读取 写入(即获取读取或写入锁定)。


Combining these you can arrange for only one thread at a time to have write access, but as many readers as you like can read at the same time except when a thread is writing.

将这些结合起来,您可以一次只安排一个线程具有写访问权限,但是除了线程正在写入之外,您可以同时读取任意数量的读者。

Put another way. Every time you want to readfrom the structure, take a readlock. Every time you want to write, take a writelock. This way whenever a write happens no-one is reading (you can imagine you have exclusive access), but there can be many readers reading at the same time so long as no-one is writing.

换一种方式。你想每次读取从结构,采取了锁。每次要写,就拿一个锁。这样,每当写入发生时,没有人正在阅读(您可以想象您拥有独占访问权限),但是只要没有人在写作,就可以有许多读者同时阅读。

回答by Jon Skeet

The documentation for ReadWriteLockmakes this clear:

的文档ReadWriteLock清楚地说明了这一点:

A ReadWriteLock maintains a pair of associated locks, one for read-only operations and one for writing. The read lock may be held simultaneously by multiple reader threads, so long as there are no writers. The write lock is exclusive.

ReadWriteLock 维护一对关联的锁,一个用于只读操作,一个用于写入。只要没有写者,读锁就可以被多个读者线程同时持有。写锁是独占的。

So you can have many readers at a time, but only one writer - and the writer will prevent readers from reading, too. This is useful if you've got some resource which is safe to read from multiple threads, and where reading is much more common than writing, but when the resource is not actuallyread-only. (If there are no writers and reading is safe, there's no need for a lock at all.)

所以你可以一次有很多读者,但只有一个作者——作者也会阻止读者阅读。如果您有一些资源可以安全地从多个线程读取,并且读取比写入更常见,但是当资源实际上不是只读时,这很有用。(如果没有作者并且阅读是安全的,则根本不需要锁定。)

回答by Eng.Fouad

When a thread acquires a WriteLock, no other thread can acquire the ReadLocknor the WriteLockof the same instance of ReentrantReadWriteLock, unless that thread releases the lock. However, multiple threads can acquire the ReadLockat the same time.

当一个线程获取WriteLock,没有其他线程可以获取ReadLock,也没有WriteLock相同的实例中ReentrantReadWriteLock,除非该线程释放锁。但是,多个线程可以同时获取ReadLock

回答by Arnav Rao

Using ReadWriteLock, you can improve performance of an application in which more reads are performed on a shared object than writes.

使用 ReadWriteLock,您可以提高对共享对象执行的读取多于写入的应用程序的性能。

ReadWriteLock maintains two locks for read and write operations. Only one lock either read or write can be acquired at the same time. But multiple threads can simultaneously acquire read lock provided write lock is not acquired by any thread.

ReadWriteLock 为读写操作维护两个锁。只能同时获取一个读锁或写锁。但是多个线程可以同时获取读锁,前提是任何线程都没有获取写锁。

ReentrantReadWriteLock is an implementation of ReadWriteLock. It gives write lock to the longest waiting thread if multiple thread are not waiting for read lock. If multiple threads are waiting for read lock, read lock is granted to them.

ReentrantReadWriteLock 是 ReadWriteLock 的一个实现。如果多个线程没有等待读锁,它会为最长的等待线程提供写锁。如果多个线程正在等待读锁,则授予他们读锁。

A reader which acquired read lock can reacquire read lock, similarly, writer can reacquire write lock and can acquire read lock also.

获得读锁的读者可以重新获得读锁,同样,写者可以重新获得写锁,也可以获得读锁。

See http://www.zoftino.com/java-concurrency-lock-and-condition-examples

请参阅http://www.zoftino.com/java-concurrency-lock-and-condition-examples

回答by ajain

Consider a situation: In a case when data structures are read-mostly- they are mutable and are sometimes modified, but most accesses involve mostly reading, so in these case we can relax the locking mechanism in away that we can allow multiple readers to access the data structures instead of readers waiting while one reader has released the lock. As long as each thread is guaranteed an up to date view of the shared data and no thread modifies it while the readers are viewing it, there will no problems. This is what read write allows : a resource can be accessed by multiple readers or a single writer at a time, but not both.

考虑一种情况:在数据结构是read-mostly- 它们是可变的并且有时会被修改的情况下,但大多数访问主要涉及读取,因此在这种情况下,我们可以放松锁定机制,以便允许多个读取器访问数据结构而不是读者在一个读者释放锁时等待。只要保证每个线程都有共享数据的最新视图,并且在读者查看时没有线程修改它,就不会有问题。这就是读写所允许的:一个资源可以被多个读者或一个作者一次访问,但不能同时被多个读者访问。