Java Reference
In-Depth Information
14.12.1. Don't stop
We mentioned in Chapter 12 that two forms of asynchronous exceptions
are definedinternal errors in the Java virtual machine, and exceptions
caused by the invocation of the deprecated Thread.stop method.
As a general rule we don't discuss deprecated methods in this topic, but
stop is specialpartly because of what it does, but mostly because it is,
as they say, "out there" and you may well come across code that uses
it (or tries to).
The stop method causes an asynchronous ThreadDeath exception to be
thrown in the thread on which it is invoked. There is nothing special
about this exceptionjust like any other exception it can be caught, and if
it is not caught it will propagate until eventually the thread terminates.
The exception can occur at almost any point during the execution of a
thread, but not while trying to acquire a lock.
The intent of stop was to force a thread to terminate in a controlled man-
ner. By throwing an exception that was unlikely to be caught, it allowed
the normal cleanup procedure of using finally clauses to tidy up as the
thread's call stack unwound. But stop failed to achieve this in two ways.
First, it couldn't force any thread to terminate because a thread could
catch the exception that was thrown and ignore ithence it was ineffect-
ive against malicious code. Second, rather than allowing a controlled
cleanup, it actually allowed the corruption of objects. If stop was invoked
while a thread was in a critical section, the synchronization lock would
be released as the exception propagated, but the object could have been
left in a corrupt state due to the partial completion of the critical section.
Because of these serious flaws stop was deprecated. Instead, interrupt
should be used for cooperative cancellation of a thread's actions.
A second form of stop took any THRowable object as an argument and
caused that to be the exception thrown in the target threadthis is even
more insidious because it allows "impossible" checked exceptions to be
thrown from code that doesn't declare them.
 
Search WWH ::




Custom Search