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

JavaMultithreadingLockingDeadlockReentrantreadwritelock

Java Problem Overview


What I know is:

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

Java Solutions


Solution 1 - Java

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.

Solution 2 - Java

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.)

Solution 3 - Java

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.

Solution 4 - Java

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

Solution 5 - Java

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDunkOnlyView Question on Stackoverflow
Solution 1 - JavaOldCurmudgeonView Answer on Stackoverflow
Solution 2 - JavaJon SkeetView Answer on Stackoverflow
Solution 3 - JavaEng.FouadView Answer on Stackoverflow
Solution 4 - JavaArnav RaoView Answer on Stackoverflow
Solution 5 - JavaajainView Answer on Stackoverflow