Thread.interrupt()

Jeff Sturm jsturm@sigma6.com
Tue Mar 14 07:36:00 GMT 2000


Miles Sabin wrote:
> 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,
[...]
> > 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
> > ().

That sounds like trading one set of underspecified behavior for
another.  I haven't tested myself this on any range of platforms.

Some stream classes are virtual, like PipedReader.  It blocks on
object.wait().   Waking it up with an interrupt would be easy enough. 
It can be made to respond to a close() as well.

Currently in libgcj, we have:

                // Note that JCL doesn't say this is the right thing
                // to do.  Still, it feels right, and we must deal
                // with an interrupt somehow.
                try
                  {
                    lock.wait();
                  }
                catch (InterruptedException e)
                  {
                    InterruptedIOException io
                      = new InterruptedIOException (e.getMessage());
                    io.bytesTransferred = count - toCopy;
                    throw io;
                  }
              }

in PipedReader.read().  The close() method is simply:

  public void close () throws IOException
  {
    closed = true;
  }

So the reader thread will remain blocked after close.  We could modify
close() to call lock.notifyAll(), and check the closed flag in read(). 
So at least for this class, Doug Lea's suggestion could probably be made
to work.  I don't know how easy it would be to implement throughout
java.io however.  Thoughts?

-- 
Jeff Sturm
jsturm@sigma6.com


More information about the Java mailing list