Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Thread.isInterrupted() returns false after thread has been terminated

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?

like image 427
Lovro Pandžić Avatar asked Jan 07 '14 10:01

Lovro Pandžić


1 Answers

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.

like image 191
Alexei Kaigorodov Avatar answered Oct 18 '22 05:10

Alexei Kaigorodov