[vox-tech] Java Threads: Abort not found...

Ken Herron vox-tech@lists.lugod.org
Sat, 19 Apr 2003 14:27:29 -0700


--On Friday, April 18, 2003 06:44:45 PM -0400 Mike Simons 
<msimons@moria.simons-clan.com> wrote:

> On Fri, Apr 18, 2003 at 02:32:15PM -0700, Ken Herron wrote:
>
>   I'm irritated about Java preventing one thread of forcefully stopping
> another thread... If you start a child thread that makes a blocking call
> into something, there doesn't appear to be a way for the parent thread
> to decide the child has taken too long and forcefully abort the child
> thread, or even get it to return from that blocked call.

I remember seeing some discussions about thread cancellation back when 
java was brand new. From what I understood of the situation, java's 
original thread API included thread cancellation, but it was quickly 
deprecated because the functionality was poorly specified, and in general 
it's extremely difficult to do thread cancellation properly.

Looking at the java 1.4.1 platform API spec, the thread class includes 
stop(), suspend(), and resume() methods, but they're all marked 
deprecated. They include a page explaining why they're deprecated, for 
example:

> 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.

I think there's a consensus that forcibly making a thread disappear is a 
"hard" problem, because cleaning up the thread's resources can be 
extremely complex. The issue isn't specific to java; it applies to 
threads in the abstract and can even be seen with processes. The only 
practical method is to notify the thread to shut itself down and hope it 
pays attention. Toward that end java has an interrupt() method you can 
use to tell a thread to shut down.

-- 
Ken Herron