Threads and lifecycle question 2 of 180 junior
Why is Thread.stop() deprecated, and what replaced it?
- It was too slow
- Correct: It released every held lock at an arbitrary point, leaving shared objects half-updated
- It only worked on Windows
- It was replaced by
Thread.destroy()
Thread.stop() threw a ThreadDeath error into the target wherever it happened to be, and unwound
its stack. Every monitor it held was released, so any object it was halfway through mutating stayed
broken and was now visible to everyone else with no exception anywhere to explain it. There is no way
to write code that is safe against that, which is why it was degraded to throwing
UnsupportedOperationException in Java 20.
The replacement is cooperative: interrupt() sets a flag, blocking calls throw
InterruptedException, and the task decides where it is safe to stop. A task that never checks the
flag and never blocks cannot be cancelled, and that is a property of the task, not a missing JDK
feature.