Java 26 finally removes Thread.stop(), one of the oldest deprecated methods in the JDK!
This method has long been considered inherently unsafe because it does not give the target thread a chance to clean up resources, release locks safely, or complete critical sections of code. If a thread was stopped while mutating shared state, other threads could observe partially updated data and corrupted state.
What should you use instead?
Modern Java code should use cooperative cancellation instead of forcibly terminating threads, to allow them to stop safely at a controlled point in execution. Typically this means:
- using interruption (
Thread.interrupt()) - checking interruption status
- using structured concurrency or executors
- designing tasks to terminate gracefully
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.