...
A few thread APIs were introduced to facilitate thread suspension, resumption and termination but were later deprecated because of inherent design weaknesses. The Thread.stop()
method is one example. It throws a ThreadDeath
exception to stop the thread. Two cases arise:
- If
ThreadDeath
is left uncaught, it allows the execution of afinally
block which performs the usual cleanup operations. Even though clean-up statements may execute in this case, use of theThread.stop()
method is highly inadvisable primarily because of two reasons. First, the target thread cannot be forcefully stopped because an arbitrary thread may catch the thrownThreadDeath
exception and simply choose to ignore it. Second, abruptly stopping a thread results in the release of all the locks that it has acquired, violating the guarantees provided by the critical sections. Furthermore, the objects end up in an inconsistent state, non-deterministic behavior being a typical outcome.
...
Wiki Markup |
---|
Invoking {{Thread.stop()}} results in the release of all the locks a thread has acquired which may corrupt the state of the object. The thread could catch the {{ThreadDeath}} exception and use a {{finally}} block in an attempt to repair the inconsistent object, however, this requires careful inspection of all the synchronized methods and blocks because a {{ThreadDeath}} exception can be thrown anywhere in the code. Furthermore, code must be protected from {{ThreadDeath}} exceptions that may result when executing the {{catch}} or {{finally}} blocks \[[Sun 99|AA. Java References#Sun 99]\] . |
More information about deprecated methods is available in MET15-J. Do not use deprecated or obsolete methods. Also, refer to EXC09-J. Prevent inadvertent calls to System.exit() or forced shutdown for information on preventing data corruption when the JVM is abruptly shut down.
...
Wiki Markup |
---|
\[[API 06|AA. Java References#API 06]\] Class Thread, method {{stop}}, interface ExecutorService
\[[Sun 99|AA. Java References#Sun 99]\]
\[[Darwin 04|AA. Java References#Darwin 04]\] 24.3 Stopping a Thread
\[[JDK7 08|AA. Java References#JDK7 08]\] Concurrency Utilities, More information: Java Thread Primitive Deprecation
\[[JPL 06|AA. Java References#JPL 06]\] 14.12.1. Don't stop and 23.3.3. Shutdown Strategies
\[[JavaThreads 04|AA. Java References#JavaThreads 04]\] 2.4 Two Approaches to Stopping a Thread
\[[Goetz 06|AA. Java References#Goetz 06]\] Chapter 7: Cancellation and shutdown |
...