Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

What I know is:

  • ReadLock and WriteLock affect each other somehow
  • WriteLock is just like synchronized
  • ReadLock seems cannot work alone
like image 910
DunkOnly Avatar asked Aug 21 '13 09:08

DunkOnly


People also ask

What is a Readlock?

An update lock is an exclusive lock, that is, only one lock can be held at a time. It is used to prevent concurrent updates. A read lock is a shared lock, multiple readers can held a read lock simultaneously.

What is the maximum number of threads that can possess the Writelock of a ReentrantReadWriteLock at the same time?

This lock supports a maximum of 65535 recursive write locks and 65535 read locks.

What is ReentrantLock?

A ReentrantLock is owned by the thread last successfully locking, but not yet unlocking it. A thread invoking lock will return, successfully acquiring the lock, when the lock is not owned by another thread. The method will return immediately if the current thread already owns the lock.

Why is ReentrantLock needed?

Reentrant Locks are provided in Java to provide synchronization with greater flexibility. What are Reentrant Locks? The ReentrantLock class implements the Lock interface and provides synchronization to methods while accessing shared resources.


3 Answers

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.

like image 142
OldCurmudgeon Avatar answered Nov 03 '22 13:11

OldCurmudgeon


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

like image 35
Jon Skeet Avatar answered Nov 03 '22 13:11

Jon Skeet


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.

like image 8
Eng.Fouad Avatar answered Nov 03 '22 14:11

Eng.Fouad