Consider the following JUnit test:
@Test
public void testSettingInterruptFlag() throws InterruptedException {
    Thread blockingThread = new Thread(new Runnable() {
        @Override
        public synchronized void run() {
            try {
                wait();
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        }
    });
    blockingThread.start();
    blockingThread.interrupt();
    blockingThread.join();
    assertTrue(blockingThread.isInterrupted());
}
The test fails even though the interrupted status was set.
I've suspected that the line blockingThread.join(); resets the flag but even when replacing that line with Thread.sleep(3000); the test still fails. Is this behaviour documented anywhere?
what we see is that interrupted status of a thread is cleared when that thread finishes. It is not documented in http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html, so can be considered as a spec or implementation bug.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With