Quick revision
The short answer
StampedLock is not reentrant: it does not track which thread holds it, so when a method that holds the write lock calls a helper that asks for the write lock again, the helper waits for the lock to be released, which is exactly what its own caller will never do, and the thread hangs. StampedLock also has no Condition support, its locks are not owned by threads so stamps can be misused across threads, and code inside an optimistic read may see inconsistent values and must not act or throw on them before validation. It belongs only in small, self-contained classes with simple call graphs.
What to remember
In this answer, we saw that the service hangs because StampedLock is not reentrant: a thread holding the write lock that calls a helper which asks for the write lock again waits for itself forever, and thread dumps do not name it as the owner. The fix is to lock once at the public boundary and use private helpers that assume the lock is held. StampedLock also lacks Condition support, has no owner thread so stamps can be misused across threads, requires optimistic-read code to copy values and not act on them before validation, and has no fair mode, so it belongs only in small, self-contained classes with simple call graphs.
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.
StampedLock is not reentrant: it does not track which thread holds it, so when a method that holds the write lock calls a helper that asks for the write lock again, the helper waits for the lock to be released, which is exactly what its own caller will never do, and the thread hangs. StampedLock also has no Condition support, its locks are not owned by threads so stamps can be misused across threads, and code inside an optimistic read may see inconsistent values and must not act or throw on them before validation. It belongs only in small, self-contained classes with simple call graphs.
Background to this question
Teams usually move from ReentrantReadWriteLock to StampedLock for one reason: optimistic reads are much faster. The API looks similar, so the switch seems mechanical. It is not, because StampedLock gives up several guarantees that the older lock provided silently, and the first one people discover is reentrancy, usually as a production hang. In this answer, we will recap what reentrancy means, reproduce the hang, see how it shows in a thread dump, fix it, and then go through StampedLock's other limitations and where it is still the right tool.
1A Quick Recap of Reentrancy
A lock is reentrant when the thread that already holds it can acquire it again without blocking. ReentrantLock and ReentrantReadWriteLock record the owning thread and a hold count: the second lock() by the owner just increments the count, and the lock is released when the count returns to zero. synchronized works the same way. That is why code that calls one locked method from another locked method on the same object works without anyone thinking about it.
StampedLock deliberately does not track an owner. Its state is just a version number and a reader count, which is part of what makes it fast and small. Without an owner, it cannot know that the thread asking for the lock is the one already holding it.
2Reproducing the Hang
Here is the shape of code that worked with ReentrantReadWriteLock and hangs after the switch.
5 messages in order. Play them, or step through them one at a time.
StampedLock has no owner thread, so it cannot recognise its own holder.
Calling adjust() directly works. Calling transfer() hangs on the first line of adjust(): the write lock is held, so writeLock() waits for it to be released, and the only thread that could release it is the one now waiting. There is no exception and no timeout. The same happens if a method holding the read lock calls a helper that asks for the write lock, or if a write lock holder asks for readLock().
3How It Looks in a Thread Dump
In a thread dump the thread is parked inside StampedLock.writeLock, called from adjust, called from transfer. Because StampedLock has no owner, the dump's 'Locked ownable synchronizers' section does not list it, so no tool points out that the waiting thread is also the holder. The clue is the stack itself: the same lock taken in two frames of one thread. Taking two or three dumps a few seconds apart shows the thread stuck in the same place.
4Fixing It: Lock at the Boundary, Not in Helpers
The fix is to make sure each public operation takes the lock exactly once. A common way is to split every operation into a public method that locks and a private method that assumes the lock is held.
Notice that the call graph is now simple: public methods lock, private methods do not. If the class is too large or has too many paths for that rule to be checked by eye, it is a sign that StampedLock is the wrong choice and ReentrantReadWriteLock or ReentrantLock should come back.
5The Other Limitations
Reentrancy is only the first difference, and the others follow from the same design choice of having no owner. A Condition works by releasing the owner's lock during await() and giving it back afterwards, so a lock without an owner cannot offer one. That is why StampedLock has no newCondition(), and code that used writeLock().newCondition() to wait for a state change cannot be ported directly; it needs a different signalling mechanism or a different lock.
Its locks are not owned by threads. Any thread can release a lock with the right stamp, and nothing stops a stamp from being passed to, or leaked to, another thread. That makes some designs possible, such as acquiring in one thread and releasing in another, but it also means a release from the wrong thread is never detected. StampedLock only rejects a stamp that does not match its current state, with IllegalMonitorStateException; ReentrantLock, by contrast, refuses any unlock from a thread that is not the owner.
Optimistic reads run without any lock, so the code inside them may see inconsistent values, a half-updated pair of fields, or a reference that is null or points to an object mid-change. Such code must only copy values and must not throw, loop or call out based on them until validate(stamp) returns true.
Finally, its blocking acquisitions are not interruptible unless we use readLockInterruptibly() or writeLockInterruptibly(), and it has no fair mode. Together these points explain the usual guidance: StampedLock is for small, self-contained classes with simple call graphs, such as a Point or a small value holder, where the optimistic read gives a measured benefit.
| Feature | ReentrantReadWriteLock | StampedLock |
|---|---|---|
| reentrant | yes, per-thread hold counts | no: second acquire by holder hangs |
| Condition support | yes, on the write lock | no |
| owner thread | yes, misuse throws | no, any thread with the stamp may unlock |
| optimistic reads | no | yes, must copy then validate |
| fair mode | optional | none |
| read to write conversion | not supported | tryConvertToWriteLock, may return 0 |
6Follow-up questions
Could a test have caught the hang before production?
Yes, a test that exercises every public method, including transfer(), with a timeout, for example JUnit's assertTimeoutPreemptively. The hang is deterministic on that path, so any test that reaches it hangs every time. The bug shipped because only some call paths were tested after a mechanical switch.
Why would anyone want a lock without an owner?
Ownership tracking costs memory and work on every acquisition. Without it, StampedLock is smaller and its optimistic reads write no shared memory, which is where its speed comes from. It also allows handing a held lock between threads in rare designs.
Is it ever right to switch back from StampedLock?
Often. If the class has a complex call graph, needs conditions or reentrancy, or the benchmark gain at service level is small, ReentrantReadWriteLock or a plain lock is the better engineering choice. Speed that comes with hangs is not a gain.
7Conclusion
In this answer, we saw that the service hangs because StampedLock is not reentrant: a thread holding the write lock that calls a helper which asks for the write lock again waits for itself forever, and thread dumps do not name it as the owner. The fix is to lock once at the public boundary and use private helpers that assume the lock is held. StampedLock also lacks Condition support, has no owner thread so stamps can be misused across threads, requires optimistic-read code to copy values and not act on them before validation, and has no fair mode, so it belongs only in small, self-contained classes with simple call graphs.
Open with
The hang is because StampedLock is not reentrant. ReentrantReadWriteLock records the owning thread and a hold count, so a second acquire by the same thread just increments the count.
Then, if they want more
StampedLock has no owner; it only has a version and a reader count. So when transfer holds the write lock and calls adjust, which calls writeLock again, the lock is simply held, and the thread waits for it to be released by itself.
It parks forever with no exception, and because there is no owner, the thread dump does not list the lock under the thread; I would spot it from the same lock in two frames of one stack. The fix is to lock once at the public method and move the work into private helpers that assume the lock is held.
Other limitations: it has no Condition support, so code waiting on conditions must be redesigned. Locks are not owned by threads, so any thread with the stamp can unlock, and misuse is not caught the way IllegalMonitorStateException catches it in ReentrantLock.
Optimistic reads take no lock, so the code inside may see inconsistent values and must only copy them, never act or throw on them before validate returns true. It also has no fair mode, and blocking acquisition is not interruptible unless I use the interruptible variants.
So I only use StampedLock in small, self-contained classes with a simple call graph and a measured benefit.
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 "A service that switched from ReentrantReadWriteLock to StampedLock started hanging under...": https://scrivio-interview-library.netlify.app/java/locks/dc9050905c/