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

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 … Read more

Java ReentrantReadWriteLocks – how to safely acquire write lock when in a read lock?

This is an old question, but here’s both a solution to the problem, and some background information. As others have pointed out, a classic readers-writer lock (like the JDK ReentrantReadWriteLock) inherently does not support upgrading a read lock to a write lock, because doing so is susceptible to deadlock. If you need to safely acquire … Read more