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

元气小坏坏 提交于 2019-12-17 08:22:21

问题


What I know is:

  • ReadLock and WriteLock affect each other somehow
  • WriteLock is just like synchronized
  • ReadLock seems cannot work alone

回答1:


readLock.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();

  • This means that if any other thread is reading or writing, 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 or write (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 read from the structure, take a read lock. Every time you want to write, take a write lock. 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.




回答2:


The documentation for ReadWriteLock makes this clear:

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.

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 actually read-only. (If there are no writers and reading is safe, there's no need for a lock at all.)




回答3:


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




回答4:


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

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.

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.

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



来源:https://stackoverflow.com/questions/18354339/reentrantreadwritelock-whats-the-difference-between-readlock-and-writelock

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