Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  • As a remediation measure, catching the ThreadDeath exception on the other hand can itself ensnarl multithreaded code. For one, the exception can be thrown anywhere, making it difficult to trace and effectively recover from the exceptional condition. Also, there is nothing stopping a thread from throwing another ThreadDeath exception while recovery is in progress.

Why is Thread.stop deprecated?

Because it is inherently unsafe. Stopping a thread causes it to unlock all the monitors that it has locked. (The monitors are unlocked as the ThreadDeath exception propagates up the stack.) If any of the objects previously protected by these monitors were in an inconsistent state, other threads may now view these objects in an inconsistent state. Such objects are said to be damaged. When threads operate on damaged objects, arbitrary behavior can result. This behavior may be subtle and difficult to detect, or it may be pronounced. Unlike other unchecked exceptions, ThreadDeath kills threads silently; thus, the user has no warning that his program may be corrupted. The corruption can manifest itself at any time after the actual damage occurs, even hours or days in the future.

Couldn't I just catch the ThreadDeath exception and fix the damaged object?

In theory, perhaps, but it would vastly complicate the task of writing correct multithreaded code. The task would be nearly insurmountable for two reasons:

1. A thread can throw a ThreadDeath exception almost anywhere. All synchronized methods and blocks would have to be studied in great detail, with this in mind.
2. A thread can throw a second ThreadDeath exception while cleaning up from the first (in the catch or finally clause). Cleanup would have to repeated till it succeeded. The code to ensure this would be quite complex.

In sum, it just isn't practical.
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.

...