Locksmedium3-5 years
Why would you choose ReentrantLock over synchronized?
For tryLock (non-blocking or with a timeout), lockInterruptibly, fairness, multiple Conditions per lock, introspection, and because virtual threads unmount while blocked on it (before Java 24 they pin on synchronized). The cost: you must unlock() in finally, every time.
PreviousWhen would you use synchronized, and when volatile?Next Explain ThreadPoolExecutor's parameters and how a task moves through them.
How does AtomicInteger work without a lock, and when does LongAdder win?A service stops answering at 02:14, right after its rates provider returned a 503. CPU is idle, no errors are logged, and the thread dump shows no deadlock. Every request thread is `WAITING (parking)` on the same `ReentrantLock$NonfairSync`, and the scheduler thread that last called `refresh()` is alive and sleeping. What happened, how do you find the owner, and what are the two fixes?