Quick revision
The short answer
ReentrantReadWriteLock has two linked locks: many threads can hold the read lock at the same time, while the write lock is exclusive and keeps out both readers and other writers. It supports downgrading, where a thread holding the write lock takes the read lock and then releases the write lock, but not upgrading: a thread holding the read lock that asks for the write lock waits forever. Both locks are reentrant, and the lock can be created in fair or non-fair mode.
What to remember
In this answer, we saw that ReentrantReadWriteLock lets many readers share the read lock while the write lock is exclusive and excludes readers. It supports downgrading, by acquiring the read lock while still holding the write lock and then releasing the write lock, but not upgrading, because a reader asking for the write lock waits for all readers, including itself, and hangs forever. Both locks are reentrant, and the lock offers fair and non-fair modes, with non-fair as the faster default.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
ReentrantReadWriteLock has two linked locks: many threads can hold the read lock at the same time, while the write lock is exclusive and keeps out both readers and other writers. It supports downgrading, where a thread holding the write lock takes the read lock and then releases the write lock, but not upgrading: a thread holding the read lock that asks for the write lock waits forever. Both locks are reentrant, and the lock can be created in fair or non-fair mode.
Why this question is asked
Read-mostly data, such as configuration, caches and lookup tables, is very common, and a plain lock makes readers queue even though they do not interfere with each other. A read-write lock is the textbook answer to that, and it comes with rules about switching between the two modes that are easy to get wrong. Interviewers ask about upgrade and downgrade because the wrong one hangs a thread with no error at all. In this answer, we will recap the idea of a read-write lock, look at how ReentrantReadWriteLock behaves for readers and writers, go through downgrading with code, see exactly why upgrading deadlocks, and finish with reentrancy and fairness.
1A Quick Recap of Read-Write Locks
Two readers of the same data cannot harm each other, because neither changes anything. Only writes need exclusive access. A read-write lock uses that fact: it allows any number of readers at once, or exactly one writer, but never a reader and a writer together. In java.util.concurrent.locks, ReadWriteLock is the interface and ReentrantReadWriteLock is the main implementation. It gives us two Lock objects, readLock() and writeLock(), which share one internal state.
Both locks follow the usual pattern: acquire immediately before try, release in finally. The visibility guarantee is the same as for other locks: whatever a writer did before releasing the write lock is visible to a thread that later acquires the read lock.
2How Readers and Writers Interact
While no thread holds the write lock, any thread that asks for the read lock gets it, so ten readers can run get() in parallel. A thread asking for the write lock waits until every read lock and any write lock has been released. While a writer holds the write lock, new readers wait.
There is one more rule in the default, non-fair mode that affects writers: if the thread at the head of the wait queue is a writer, new readers queue behind it rather than jumping in. That heuristic stops writers from being starved completely by a constant stream of readers, though it does not give strict ordering.
| Thread A holds | Thread B asks for | Result for B |
|---|---|---|
| read lock | read lock | granted at once |
| read lock | write lock | waits until all readers leave |
| write lock | read lock | waits until the writer leaves |
| write lock | write lock | waits |
| nothing | either lock | granted |
3Downgrading: From Write to Read
Downgrading is supported, and it is useful. Suppose a thread updates the data and then wants to keep reading it, sure that no other writer changes it in between, while still letting other readers in. It acquires the read lock while it still holds the write lock, which is allowed because it is the writer, and then releases the write lock. From that moment it is an ordinary reader.
Notice the order: read lock first, then release the write lock. If we released the write lock first and then asked for the read lock, another writer could slip in between, and we would read data that is not the data we just wrote.
4Upgrading: Why Read to Write Hangs
Upgrading is the opposite move: a thread holding the read lock asks for the write lock, for example after a cache miss. ReentrantReadWriteLock does not support this. The write lock can only be granted when there are no readers, and the thread asking is itself a reader. It waits for all read locks to be released, including its own, which it will never release because it is waiting. The thread hangs forever, with no exception.
- Thread holds the read lock
- Thread calls writeLock().lock()
- Any readers still holding the lock?YesWait, and the thread itself is one of themNoGrant the write lock
- Thread waits for itself foreverno timeout, no exception
4 steps in order. Play them, or step through them one at a time.
There is also a deeper reason the lock refuses upgrades. If two readers both tried to upgrade at the same time, each would wait for the other to release its read lock, a true deadlock between two threads. The correct way is to release the read lock, acquire the write lock, check the condition again, because another thread may have changed it in the gap, and then write.
5Reentrancy and Fairness
Both locks are reentrant. A thread holding the read lock can acquire it again, and a thread holding the write lock can acquire the write lock again, or the read lock, as in downgrading. Each acquisition needs its own unlock. The class has inspection methods such as getReadHoldCount(), getWriteHoldCount() and isWriteLocked() that help in tests.
The constructor takes a fairness flag. new ReentrantReadWriteLock() is non-fair: it does not promise any order and gives higher throughput. new ReentrantReadWriteLock(true) is fair: threads acquire roughly in arrival order, so a waiting writer blocks later readers and a group of waiting readers gets in after the writer leaves. Fair mode reduces starvation at a cost in throughput.
6Follow-up questions
Can the read lock be used with a Condition?
No. readLock().newCondition() throws UnsupportedOperationException, because waiting for a condition while other readers share the lock does not make sense. The write lock supports conditions, just like a ReentrantLock.
Is a read-write lock always faster than a plain lock for read-mostly data?
No. Taking the read lock still updates shared state with an atomic operation, so for very short critical sections that overhead can make it slower than a simple lock. It pays off when reads take long enough and are frequent enough that running them in parallel matters. Benchmark with real ratios before switching.
Is there a lock that supports upgrading?
StampedLock has tryConvertToWriteLock(stamp), which upgrades when it can and returns 0 when it cannot, so the caller must handle the failure by releasing and acquiring the write lock. It is not reentrant, so it has its own sharp edges.
7Conclusion
In this answer, we saw that ReentrantReadWriteLock lets many readers share the read lock while the write lock is exclusive and excludes readers. It supports downgrading, by acquiring the read lock while still holding the write lock and then releasing the write lock, but not upgrading, because a reader asking for the write lock waits for all readers, including itself, and hangs forever. Both locks are reentrant, and the lock offers fair and non-fair modes, with non-fair as the faster default.
Open with
ReentrantReadWriteLock gives me two locks that share state: a read lock that many threads can hold at the same time, and a write lock that is exclusive, so while a writer holds it there are no readers and no other writers.
Then, if they want more
A writer has to wait until every reader has left. It supports downgrading: a thread that holds the write lock can take the read lock, which is allowed because it is the writer, and then release the write lock, so it keeps reading the data it just wrote while other readers can come in but no writer can.
The order matters; if I released the write lock first, another writer could slip in. It does not support upgrading. If a thread holding the read lock asks for the write lock, it waits for all readers to leave, including itself, so it hangs forever without any exception.
The correct way is to release the read lock, take the write lock, and check the condition again because something may have changed in between. Both locks are reentrant, with separate hold counts, and only the write lock supports conditions.
Finally, it has a non-fair default mode with better throughput, and a fair mode that grants the lock roughly in arrival order to reduce starvation of writers.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- java.util.concurrent, package documentation, JLS chapter 17, Threads and Locks
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "How does ReentrantReadWriteLock behave, and which of lock upgrade and lock downgrade...": https://scrivio-interview-library.netlify.app/java/locks/5a0b02653a/