Note that there are some explanatory texts on larger screens.

plurals
  1. POThread not throwing an InterruptedException when interrupt() is called
    primarykey
    data
    text
    <p>I am working on an application that reads and processes data over a network. While testing the connecting/disconnecting logic of the program I noticed that my consumer thread was not closing when it reached it's closing condition. Below is a stripped out version of the consumer class.</p> <pre><code>import java.io.InputStream; public class Consumer implements Runnable { private final InputStream input; public Consumer(InputStream input) { this.input = input; } @Override public void run() { byte readBuffer[]; readBuffer = new byte[1]; int goodData; try { while(input.available() &gt; 0) { goodData = input.read(readBuffer); while (goodData &gt; 0 ) { System.out.println(readBuffer[0]); if ( readBuffer[0] == 27 ) { System.out.println("Consumer: found closing byte and closing thread "+Thread.currentThread().getName()); //this is the last packet, so interupt thread to close Thread.currentThread().interrupt(); //return; //Thread.currentThread().stop(new InterruptedException("Attempting to close")); } goodData = input.read(readBuffer); } } } catch(Exception e) { System.out.println("closing "+Thread.currentThread().getName() +" because of an exception "+e.getClass()); return; } System.out.println("closing "+Thread.currentThread().getName()); } } </code></pre> <p>I created a dummy main class that demonstrates the problem.</p> <pre><code>public class ExampleOfInterruptNotWorking { public static void main(String[] args) { byte[] bytesToWrite = new byte[]{0, 1, 2,3,4,5,6,65,23,65,21,54,13,54,1,76}; Consumer C; Thread ConsumerThread; PipedInputStream PIS = null; PipedOutputStream POS = null; try { PIS = new PipedInputStream(); POS = new PipedOutputStream(PIS); C = new Consumer(PIS); ConsumerThread = new Thread(C); ConsumerThread.start(); POS.write(bytesToWrite); POS.write(bytesToWrite); bytesToWrite[1] = 27; POS.write(bytesToWrite); ConsumerThread.join(); } catch(Exception e) { System.err.println("Unexpected exception in main"); e.printStackTrace(System.err); } finally { try { PIS.close(); POS.close(); } catch(Exception ex) { //shouldn't happen in example } System.out.println("exiting main"); } } } </code></pre> <p>When you run this code as written, the consumer detects the interrupt, but does not stop execution until the pipe is empty (not what I want). Just to try, I changed to a Thread.stop() call which did what I wanted, but I don't want to leave that in production code. I realized that I could use a simple return statement, but this is not the only point the thread could exit, and I'd like to have some common exit code that cleans up resources. So, my question is, why is the consumer thread not being interrupted? and is there a good way for me to be able to have common exit code?</p> <p>Thanks!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload