Quick revision
The short answer
On a cache miss, the thread asks for the write lock while it still holds the read lock. ReentrantReadWriteLock only grants the write lock when no thread holds the read lock, and that includes the caller itself, so the thread waits for its own read lock to be released, which can never happen, and it hangs forever. The fix is to release the read lock, take the write lock, check the map again, load and put, and optionally downgrade; for a cache, ConcurrentHashMap.computeIfAbsent is usually simpler.
What to remember
In this answer, we saw that the cache method hangs on a miss because it asks for the write lock while still holding the read lock, and ReentrantReadWriteLock only grants the write lock when there are no readers, including the caller, so the thread waits for itself forever without any exception. The fix is to release the read lock, acquire the write lock, check the map again, then load and put, with an optional downgrade. For a cache, ConcurrentHashMap.computeIfAbsent or a cache library is usually simpler and avoids holding a global lock while loading.
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.
On a cache miss, the thread asks for the write lock while it still holds the read lock. ReentrantReadWriteLock only grants the write lock when no thread holds the read lock, and that includes the caller itself, so the thread waits for its own read lock to be released, which can never happen, and it hangs forever. The fix is to release the read lock, take the write lock, check the map again, load and put, and optionally downgrade; for a cache, ConcurrentHashMap.computeIfAbsent is usually simpler.
Why this question is asked
This hang is one of the most common read-write lock mistakes, because the code reads naturally: check under the read lock, and if the value is missing, take the write lock to add it. It also fails quietly: no exception, no deadlock report from simple tools, just a request that never returns. Interviewers like it because the explanation shows whether we know the upgrade rule and why it exists. In this answer, we will recap the code, walk through the miss step by step, see how the hang looks in a thread dump, write the correct version, and then look at the simpler alternative.
1A Quick Recap of the Code
Here is the method from the question, laid out so we can follow it. It is a read-through cache: look up the key and, on a miss, load the value and store it.
On a cache hit everything is fine: the thread takes the read lock, finds the value, returns it and releases the read lock. The problem is only on the miss path, which is why the bug can survive tests that only ever use a warm cache.
2Walking Through the Miss
The read lock is shared, the write lock is exclusive. ReentrantReadWriteLock grants the write lock only when there are no holders of the read lock at all. It does not make an exception for the thread that is asking. Now follow the thread on a miss. It holds the read lock. It finds null and calls writeLock().lock(). The write lock checks for readers and finds one, the calling thread. So the call waits for that reader to leave.
The reader can only leave by reaching the outer finally, which comes after the inner block, which is waiting. So the thread waits for itself and never continues. Unlike some misuse, this does not throw IllegalMonitorStateException or any other exception; it simply parks forever. And it gets worse: in non-fair mode, a waiting writer at the head of the queue makes new readers queue behind it, so other threads calling get() can pile up behind the stuck one too.
6 messages in order. Play them, or step through them one at a time.
The outer finally that would release the read lock is never reached.
3How the Hang Looks in a Thread Dump
If we take a thread dump with jstack -l <pid> or jcmd <pid> Thread.print -l, the stuck thread is parked inside the write lock's acquire, and its list of locked ownable synchronizers shows that it holds the same lock's read side. Seeing a thread wait for a lock it partly owns is the signature of this bug.
Notice the last line: read locks are shared, so they are not recorded as owned by one thread, and the dump shows None. That makes the bug harder to spot; the clue is a thread parked in WriteLock.lock called from inside a method that already took the read lock. In code, rw.getReadHoldCount() returning 1 just before the writeLock() call proves it, and a test can assert that it is 0 there.
4The Fix: Release, Lock, Check Again
Since an upgrade is impossible, the thread must give up the read lock before asking for the write lock. That creates a gap in which another thread may load the same key, so after acquiring the write lock we check the map again. This is the double check that every correct version of this pattern needs.
If the caller needs to keep reading under protection after the write, it can downgrade: take the read lock inside the write block and then release the write lock, which ReentrantReadWriteLock does support. Notice also that load(k) runs under the write lock, so all readers wait while one key loads. If load is slow, for example a database call, that is a second problem, and it is a reason to prefer the next option.
5The Simpler Option: ConcurrentHashMap.computeIfAbsent
For a cache, we usually do not need our own lock at all. ConcurrentHashMap.computeIfAbsent runs the loading function at most once per key when the key is absent, and it only blocks other updates to the same part of the map, not the whole map. Reads of other keys continue in parallel.
| Approach | Hits | Misses | Risk |
|---|---|---|---|
| read lock then write lock (original) | parallel | thread hangs forever | upgrade deadlock |
| release, write lock, check again | parallel | one load at a time, readers wait | slow loads block all |
ConcurrentHashMap.computeIfAbsent | lock-free | per-key, others continue | no recursive updates |
Caffeine LoadingCache | lock-free | per-key, expiry and limits | an extra dependency |
One rule applies: the loading function should be short and must not modify the same map, because it runs while part of the map is locked, and recursive updates can throw IllegalStateException or block. For slow loads, expiry or size limits, a cache library such as Caffeine is the better tool.
6Follow-up questions
Why does ReentrantReadWriteLock not just allow the upgrade when only the caller is reading?
Because if two readers tried to upgrade at the same time, each would wait for the other to give up its read lock, and both would hang. Refusing all upgrades keeps the rules simple and makes the caller handle the gap explicitly, which is safer.
Would tryLock() on the write lock avoid the hang?
writeLock().tryLock() would return false instead of waiting, so the thread would not hang, but it would still not get the write lock while holding the read lock. The code would then need a fallback, which ends up being the release and re-check pattern anyway.
How would you write a test that catches this bug?
Start with an empty cache and call get() with a timeout, for example with assertTimeoutPreemptively in JUnit or a Future.get with a timeout. The original code times out on the first miss, while the fixed version returns. A test with only pre-loaded keys would never find it.
7Conclusion
In this answer, we saw that the cache method hangs on a miss because it asks for the write lock while still holding the read lock, and ReentrantReadWriteLock only grants the write lock when there are no readers, including the caller, so the thread waits for itself forever without any exception. The fix is to release the read lock, acquire the write lock, check the map again, then load and put, with an optional downgrade. For a cache, ConcurrentHashMap.computeIfAbsent or a cache library is usually simpler and avoids holding a global lock while loading.
Open with
On a cache miss the thread is trying to upgrade from the read lock to the write lock, and ReentrantReadWriteLock does not support upgrades. The write lock is only granted when no thread holds the read lock, and the calling thread is itself holding it, because the outer finally that would release it only runs after the inner block.
Then, if they want more
So the thread waits for its own read lock to be released and parks forever. There is no exception, and in non-fair mode the waiting writer can also make new readers queue behind it, so other requests pile up. A hit works fine, which is why tests with a warm cache miss the bug.
To fix it, I would release the read lock first, then take the write lock, check the map again because another thread may have loaded the key in the gap, and only then load and put. If I need to keep reading afterwards, I can downgrade by taking the read lock before releasing the write lock, which is supported.
But for a cache I would usually not use a read-write lock at all: ConcurrentHashMap.computeIfAbsent loads each key at most once, reads are lock-free, and only that key is blocked during loading. For expiry or size limits I would use Caffeine.
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 "This cache method hangs the calling thread forever on a cache miss. Why?": https://scrivio-interview-library.netlify.app/java/locks/91fa3234c2/