1

I'm faced with strange behaviour. All sources tells that you need to call Thread.currentThread().interrupt() after you catch interrupt exception to restore interrupt flag. But i tried to create simple app that check interrupt status after process was interrupted

public class Main {
    public static void main(String[] args) throws InterruptedException {
        Runnable r = () -> {
            try {
                Thread.sleep(100000);
            } catch (InterruptedException e) {
                System.out.println("interrupted exception is occurred");
                Thread.currentThread().interrupt();
                System.out.println("interrupt flag is updated:" + Thread.currentThread().isInterrupted());
            }
        };

        Thread thread = new Thread(r);
        thread.start();
        thread.interrupt();

        boolean interruptFlag;
        do {
            Thread.sleep(1000);
            interruptFlag = thread.isInterrupted();
            System.out.println("interrupt flag:" + interruptFlag + " isAlive:" + thread.isAlive());
        } while (!interruptFlag);
    }
}

If you try ti run it then you get

interrupted exception is occurred
interrupt flag is updated:true
interrupt flag:false isAlive:false
interrupt flag:false isAlive:false
interrupt flag:false isAlive:false
interrupt flag:false isAlive:false
interrupt flag:false isAlive:false

It will work infinitely. The question is why iterrupt flag is false after the thread is dead?

  • @MuratK. Wrong duplicate. OP changes the status with `Thread.currentThread().interrupt();` – Oleg Nov 03 '17 at 09:11
  • `interruptFlag` needs to be `volatile`, and a thread that is no longer alive can't be interrupted. – user207421 Nov 03 '17 at 09:13
  • No, It's over case, if you look at catch you will see that i restore interrupt flag. – pkokoshnikov Nov 03 '17 at 09:13
  • Not alive isn't iterrupted, the code sneep start thread, then interrupts it and then checks the flag – pkokoshnikov Nov 03 '17 at 09:15
  • @EJP Local variable can't and doesn't need to be volatile. OP sets the interrupt flag to true while the Thread is alive. So far I can't find the documentation that interrupt flag is reset on thread exit. – Oleg Nov 03 '17 at 09:18
  • I tried to transform interruptFlag to volatile static field but it doesn't work – pkokoshnikov Nov 03 '17 at 09:20
  • Yes, I also couldn't find such documenation and thats why i decided to ask, may be someone knows about this behaviour. From my view point it looks like a bug – pkokoshnikov Nov 03 '17 at 09:21
  • on KitKat isAlive flag always returns true even after interrupt – Duna Jun 07 '20 at 13:30

1 Answers1

1

When you check interrupted status from main thread the second thread is already dead so it returns false. Try to place Thread.sleep(1000) in the end of the waiting loop and you will see true in interrupted status.

This behavior isn't documented so it's hard to say whether it's lack of documentation or a feature of existing implementation. Implementation of private native boolean isInterrupted(boolean ClearInterrupted); method need to be checked to say more.

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

Mikita Harbacheuski
  • 2,193
  • 8
  • 16
  • Yes, I thought about it. But i didn't find it in documentation – pkokoshnikov Nov 03 '17 at 09:34
  • Why does it matter that it's dead? Where is it specified that interrupt flag is cleared on thread exit (or that `isInterrupted` always return flast when the thread is dead)? – Oleg Nov 03 '17 at 09:43
  • This behavior isn't documented so it's hard to say whether it's lack of documentation or a feature of existing implementation. I need to check implementation of private native boolean isInterrupted(boolean ClearInterrupted); method to say more. Similar discussion is here : https://stackoverflow.com/questions/20969486/thread-isinterrupted-returns-false-after-thread-has-been-terminated – Mikita Harbacheuski Nov 03 '17 at 09:59
  • That is the correct duplicate, it's unfortunate that OP accepted the incorrect one. Please edit your comment into your answer. – Oleg Nov 03 '17 at 10:06