Thread.interrupt()

Boehm, Hans hans_boehm@hp.com
Tue Mar 14 09:57:00 GMT 2000


It seems to me that there are a bunch of problems with leaving out
interruptable
I/O:

- As Jeff and the Sun bug report point out, there doesn't seem to be a
portable alternative.

- The closest alternative, closing the descriptor, doesn't feel like
reasonable programming
practice.  If I want to stop a worker thread I created, it's not necessarily
my business to
know what file descriptor it's waiting on.  The "technique", if I understand
it correctly,
seems to be a gross violation of modularity.

- I view gcj's goal as providing a Java implementation that is particularly
interoperable
with C/C++.  In the Posix world, there is an approximate equivalent for
thread.interrupt:
pthread_cancel.  (Based on a quick look at the source, recent versins seem
to try to do the
right thing on Linux, in spite of the documentation.)  I think this makes it
highly
desirable that pthread_cancel should work on Java threads.  Once you accept
that, it seems
hard to believe that Thread.interrupt() can't do the right thing.  It seems
to me that
Thread.interrupt() should in fact be implemented (on Posix platforms) using
pthread_cancel,
though it may take some work on the pthreads side to make that fly.

Hans


-----Original Message-----
From: Miles Sabin [ mailto:msabin@cromwellmedia.co.uk ]
Sent: Tuesday, March 14, 2000 6:00 AM
To: java-discuss@sourceware.cygnus.com
Subject: RE: Thread.interrupt()


Jeff Sturm wrote,
> IOW, Sun crippled their API because Win32 doesn't handle 
> interruptable I/O portably.  Not a good policy.
>
> Since Java doesn't support read() with timeouts either, it is 
> easy to create a "stuck" thread that can never die.

The following posting of Doug Lea's on this topic from a while 
back on the advanced-java list is the best response to your
complaint ...

Doug Lea wrote,
> I think the consensus is that Interruptible I/O was a
> reasonable-looking notion that turned out to be a bad idea in
> practice. In fact, I hope it becomes deprecated before it is 
> fully implemented. (Currently, it is very incompletely 
> implemented, at least on Solaris and Win platforms.) The 
> fundamental problem is that there is rarely a reasonable 
> continuation action that can be taken if low-level I/O 
> interrupts.  Most Java programs use buffered or translated 
> I/O classes, which in turn rely on lower-level I/O classes. 
> But there is hardly ever a way for them to roll back or
> forward under a lower level exception. So the only recourse 
> is to abort.
>
> Given this (see http://gee.cs.oswego.edu/dl/cpj/cancel.html ), 
> usually the best way to implement cancellation in I/O is just 
> resource revocation -- asynchronously forcibly closing the 
> stream that the thread is operating on. This results in a 
> generic I/O exception, which can then be used to shut down a 
> thread. This works well (at least on Solaris 1.2. I don't 
> know about other platforms). To play it safe, and force 
> termination whether the thread is actually doing any I/O, you
> should normally do both stream.close() and Thread.interrupt
> ().

Cheers,


Miles

-- 
Miles Sabin                       Cromwell Media
Internet Systems Architect        5/6 Glenthorne Mews
+44 (0)20 8817 4030               London, W6 0LJ, England
msabin@cromwellmedia.com          http://www.cromwellmedia.com/


More information about the Java mailing list