Memory modelmedium0-2 years
How do you cancel a thread, and what must you do when you catch InterruptedException?
There is no way to stop a thread from outside; Thread.stop is gone. Cancellation is cooperative: interrupt() sets a flag, blocking methods throw InterruptedException, and non-blocking code should check isInterrupted(). When you catch it, either exit or restore the flag with Thread.currentThread().interrupt() — swallowing it makes the task un-cancellable.
PreviousHow does AtomicInteger work without a lock, and when does LongAdder win?Next 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?