Explicit locks
20 questions in 6 subtopics.
ReentrantLock
Show the correct usage pattern forReentrantLock and list features it offers that synchronized does not.What is wrong with this code?try {
lock.lock();
if (!valid(order)) return;
process(order);
} finally {
lock.unlock();
}
lock() throws or is interrupted, and compare with placing lock() before try.On a Java 25 codebase, how would you choose between synchronized and ReentrantLock for new code, including when virtual threads are used?A new release of a shared client library introduced a global ReentrantLock around token refresh, and p99 latency rose tenfold across many services. You lead the incident. Describe the response and the follow-up changes.Over 15 years your codebase has accumulated synchronized blocks, ReentrantLocks, ReadWriteLocks, StampedLocks and custom lock wrappers. What would you standardise or forbid in locking, and how do you weigh consistency against letting experts optimise?ReadWriteLock
How doesReentrantReadWriteLock behave, and which of lock upgrade and lock downgrade does it support?This cache method hangs the calling thread forever on a cache miss. Why?rw.readLock().lock();
try {
V v = map.get(k);
if (v == null) {
rw.writeLock().lock();
try {
v = load(k);
map.put(k, v);
} finally {
rw.writeLock().unlock();
}
}
return v;
} finally {
rw.readLock().unlock();
}
ReentrantReadWriteLock around a read-mostly map, expecting a speedup, but benchmarks show it got slower. Another service with the same lock sees writes delayed by seconds. Explain both.A shared reference-data library used by 40 services guards a large map with a ReentrantReadWriteLock; data reloads every five minutes. Some services see request latency spikes during reloads. Redesign the library.StampedLock
Write an optimistic read usingStampedLock for a Point with fields x and y, and explain when the optimistic path falls back.A service that switched from ReentrantReadWriteLock to StampedLock started hanging under certain call paths where a method holding the write lock calls a helper that also takes the write lock. Explain and list StampedLock's other limitations.StampedLock offers fast optimistic reads but is not reentrant and is easy to misuse. How do you decide whether performance-oriented APIs with sharp edges should be allowed in a codebase that will live for ten years or more?Condition
Implement put and take for a bounded buffer usingReentrantLock with two Conditions, and explain why two conditions are better than one.You need to wait until a condition becomes true or 500 ms pass, handling spurious wakeups correctly. Write the loop using Condition.awaitNanos.Fairness
What does a fairReentrantLock guarantee, what does it cost, and does tryLock() respect fairness?Users report that a few requests wait seconds for a shared lock while most are fast. A teammate switched the lock to fair mode and p99 improved but throughput fell by 40%. How would you reason about this and what else would you try?TryLock and timeouts
Write a money transfer between two Account objects that cannot deadlock, using either lock ordering ortryLock with timeout, and explain the trade-off.A hot code path uses lock.tryLock(200, MILLISECONDS) and on failure throws an exception that becomes an HTTP 500. During traffic spikes many requests fail this way. How would you treat the timeout and the underlying problem?A service used a ReentrantLock to make sure only one reservation per seat is created. After scaling to four instances behind a load balancer, double bookings appear. Design a correct approach and discuss the distributed-lock options.In distributed systems, locks become leases with timeouts and fencing tokens. Which principles from in-JVM locking carry over, which do not, and what long-term stance on distributed locking would you take for your organisation?No question matches these choices.