6

Please explain me more the contract. I can't figure out if two locks contained in ReentrantReadWriteLock somehow related? Or these are just a bundle of two normal locks?

Dims
  • 47,675
  • 117
  • 331
  • 600

2 Answers2

10

It allows multiple threads to read a resource concurrently, but requires a thread to wait for an exclusive lock in order to write to the resource.

Rules are:

  • Several readers can share the resource concurrently. If you have a read lock, you can safely acquire another read lock. The maximum count of shared locks is 1111 1111 1111 1111
  • If you have a read lock, you CANNOT acquire a write lock
  • If you have a write lock, you CANNOT acquire a read lock in any other thread.
  • A reader can access the resource when there are no writers active in any other thread.
  • If you have a write lock, you can acquire another write lock in the same thread. The maximum count of exclusive locks that a thread can own is 1111 1111 1111 1111
  • A writer can access the resource when no other reader or writer (from a different thread) is active.
  • Prefers writers over readers. That is, if a writer is waiting on the lock, no new readers from other thread are allowed to access the resource. Existing readers can continue to use the resource until they release the lock. This prevents so-called "writer starvation".
  • Allows downgrading from the write lock to a read lock, by acquiring the write lock, then the read lock and then releasing the write lock. However, upgrading from a read lock to the write lock is not possible.

Internally the lock state (c) is maintained by an int value. In this case, since we have read and write locks, it is logically divided into two shorts: The lower one representing the exclusive (writer) lock hold count, and the upper the shared (reader) hold count.

Assuming current state of lock is c= xxxx xxxx xxxx xxxx yyyy yyyy yyyy yyyy then Number of reader locks are the upper bits xxxx xxxx xxxx xxxx

Number of writer locks are the lower bits yyyy yyyy yyyy yyyy

Satish
  • 421
  • 5
  • 5
2

If threads are waiting Read Lock it is shared but when thread wants to acquire write lock only that thread is allowed the access same as mutual exclusion.

So either one of operation is allowed .if lock is held by readers and thread request write lock no more readers are allowed to acquire read lock until thread which has acquired write lock release it.

Amit Deshpande
  • 19,001
  • 4
  • 46
  • 72
  • @Dims Yes. Read lock can be shared so multiple threads can a acquire it. – Amit Deshpande Oct 26 '12 at 18:05
  • What these multiple threads which already have acquired the lock are allowed to do? How can they operate if writer thread operates simultaneously? – Dims Oct 26 '12 at 18:06
  • @Dims It is not simultaneous. Multiple read are allowed if write is not happening but when write is happening read is not allowed. – Amit Deshpande Oct 26 '12 at 18:09
  • I mean how can this matters? Suppose, for example, that one thread acquired read lock microsecond before another thread acquired write lock. Why reader was allowed to do that and why it would not if came 2 microseconds later? What changed? – Dims Oct 26 '12 at 18:09
  • but reader thread could already be within a section when writer thread came; who will stop the reader if it is ongoing? – Dims Oct 26 '12 at 18:10
  • 2
    `ReentrantReadWriteLock` is designed so that you _can't_ acquire a write lock while someone is reading, and vice versa. That's part of the point. – Louis Wasserman Oct 26 '12 at 18:13
  • All Synchronizer in concurrent package extends from `AbstractQueuedSynchronizer` as from name you can see it maintains queue of threads which are waiting for the lock. Now when Read Lock is acquired by one thread, threads which want read access are allowed to acquire lock. But when write lock is requested all threads will be in queue to acquire read lock again. – Amit Deshpande Oct 26 '12 at 18:14
  • @Louis what is the difference with single lock then? – Dims Oct 26 '12 at 20:45
  • @AmitD can lock be robbed from reader? If reader is already reading, can write lock be acquired? If "yes" then reader will continue reading simultaneously with writing started. If "no" then what is the difference with single lock? It is said that that 2 locks are more effective, but how it can be if both are excluding each other? – Dims Oct 26 '12 at 20:53
  • @Dims The major difference is Read lock is shared among threads if you consider a case of normal lock then each thread has to acquire it. Where as in this case it is not needed for Read access. If some thread is doing read then another thread can come and do read as lock is granted to that thread. This the difference and effectiveness... – Amit Deshpande Oct 26 '12 at 20:56
  • @AmitD so if we have only pair of threads (reader and writer) then no difference exist with single usual `synchronized` word? – Dims Oct 26 '12 at 21:03
  • 1
    @Dims Yes. I that case there is no difference and I think it should be documented as well – Amit Deshpande Oct 26 '12 at 21:04
  • 1
    @Dims Yes. Once thread can acquire both the locks Please see the [Javadocs](http://docs.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/locks/ReadWriteLock.html) – Amit Deshpande Oct 26 '12 at 21:06
  • @AmitD sorry I can't understand it from apidoc http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/locks/ReentrantReadWriteLock.html ; it is written like a discussion with somebody's thoughts instead of just explaining what is happening – Dims Oct 26 '12 at 21:07
  • @AmitD can you point me directly where it is said in Javadoc that writer will wait for readers to unlock? – Dims Oct 26 '12 at 21:13
  • 1
    @Dims I guess this [link](http://tutorials.jenkov.com/java-util-concurrent/readwritelock.html) will be more clearer than java doc. Java doc implicitly states it `The write lock is exclusive.` – Amit Deshpande Oct 26 '12 at 21:19
  • 1
    You can't acquire a write lock while someone is reading, and vice versa. You can acquire a read lock while other threads are reading, so long as no threads are writing. That's the difference from a single lock. – Louis Wasserman Oct 26 '12 at 22:19
  • Where did you take a quote from your answer ? – gstackoverflow Oct 15 '19 at 13:16