> Right.  Sleeping and blocking I/O are interrupted by the interrupt
> mechanism; when this happens, the I/O methods and the sleep method
> throw an appropriate exception that tells you it is interrupted.

I/O methods do not throw, at least not on NT Sun's JDKs and API
documentation does not state they must. Sorry for repeating posting but
this is my test case from message posted earlier to this thread. Please
try it on Linux with native threads if possible: 

---------
Well, as my testing has shown even stop() function does not stop thread
waiting IO (I tested on NT Sun's JDK 1.1.7 though). I do not feel it is
thread API design disadvantage; sooner it is drawback of java.io API. My
common sense tells me: there is no way to stop thread "in correct
manner" if it has no provisions for this. InputStream makes you to poll
available() function for non-blocking read. Probably my testing code
will give you some assistance in your problem:
...

--------cut here----------
/**
 * Q. Can I interrupt input waiting?
 * A1. No -- with stop(), destroy() or interrupt().
 * A2. Yes -- with available() polling.
 */
public class IOWaiting
  {
  public static void main(String [] argv)
    {
    final Thread mainThread = Thread.currentThread();
    Thread t;
    (t = new Thread(new Runnable()
        {
        public void run()
          {
          try
            {
            Thread.currentThread().sleep(5000);
            System.out.println("Stopping main thread...");
// uncomment call you want to test
//          mainThread.interrupt();
//          mainThread.destroy();
            mainThread.stop();
            }
          catch(InterruptedException e)
            {
            System.out.println("Interrupter has been interrupted!");
            }
          }
        }
      )).start();
    System.out.println("Input something... or don't do it :)");
    int ioResult = 777;
    try
      {
      ioResult = System.in.read();
      t.interrupt();
      }
    catch(java.io.IOException e)
      { e.printStackTrace(); }
    System.out.println("IOResult:" + ioResult);
    (t = new Thread(new Runnable()
        {
        public void run()
          {
          try
            {
            Thread.currentThread().sleep(5000);
            System.out.println("Stopping main thread...");
            mainThread.interrupt();
            }
          catch(InterruptedException e)
            {
            System.out.println("Interrupter has been interrupted!");
            }
          }
        }
      )).start();
    try
      {
// clear System.in for the next test
      while(System.in.available() > 0)  ioResult = System.in.read();
      System.out.println("Input something again ... or don't do it :)");
      try
        {
        while(System.in.available() == 0)
          { // polling... but what can I do?
          Thread.currentThread().sleep(100);
          }
        ioResult = System.in.read();
        t.interrupt();
        }
      catch(InterruptedException e)
        { System.out.println("Reading has been interrupted!"); }
      }
    catch(java.io.IOException e)  { e.printStackTrace(); }
    System.out.println("IOResult:" + ioResult);
    }
  }
-----------cut here

All the Best
Pavel

Reply via email to