Locksmedium3-5 years
How does AtomicInteger work without a lock, and when does LongAdder win?
Compare-and-swap: read the value, compute the new one, write it only if the value is still what you read; on failure, retry with the fresh value. One CPU instruction, no parking. Under heavy contention many threads spin on the same cache line — LongAdder spreads increments across cells and sums on read, so it wins for write-heavy counters.
PreviousProduction requests are hanging. How do you tell a deadlock from thread-pool exhaustion?Next How do you cancel a thread, and what must you do when you catch InterruptedException?
Why would you choose ReentrantLock over synchronized?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?