isInterrupted() return false after interrupt(). How the interrupt work?

Issue

I try to consider with multithreading and with interrupt() method:

    public static void main(String[] args) throws InterruptedException {
        System.out.println(Thread.currentThread().isInterrupted());
        Runnable target  new Runnable() {
            @Override
            public void run() {
                try {
                    System.out.println("From thread-run(): " + Thread.currentThread().isInterrupted());
                    Thread.sleep(7000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    System.out.println("From thread-catch(): " + Thread.currentThread().isInterrupted());
                }
            }
        };
        Thread someThread  new Thread(target);
        System.out.println("Before start(): " + someThread.isInterrupted());
        someThread.start();
        Thread.sleep(1000);
        System.out.println("After start(): " + someThread.isInterrupted());
        someThread.interrupt();
        Thread.sleep(1000);
        System.out.println("After interrupt: " + someThread.isInterrupted());
    }

As I understood, the interrupt() method set flag interrupt to true. In this case, the thread supposed to be stopped immediately, but my code return false in all cases:

enter image description here

I changed my method in the way when the thread interrupt himself:

    public static void main(String[] args) throws InterruptedException {
        System.out.println(Thread.currentThread().isInterrupted());
        Runnable target  new Runnable() {
            @Override
            public void run() {
                    System.out.println("From thread-run(): " + Thread.currentThread().isInterrupted());
                    Thread.currentThread().interrupt();
            }
        };
        Thread someThread  new Thread(target);
        System.out.println("Before start(): " + someThread.isInterrupted());
        someThread.start();
        Thread.sleep(1000);
        System.out.println("After start(): " + someThread.isInterrupted());
        Thread.sleep(1000);
        System.out.println("After interrupt: " + someThread.isInterrupted());
    }

But the result is the same:

enter image description here

I supposed, the From thread-catch(): and After interrupt: lines should have returned true instead of false – why isInterrupted() false if the thread was interrupted?


the answer from Why do InterruptedExceptions clear a thread’s interrupted status? do not acceptable for my case since in the second case the InterruptException do not happen and

  • a) not “would have to explicitly clear that flag”:
  • b) I used Thread.currentThread().interrupt() – anyway I got false
  • c) The interrupt() was handled once

enter image description here

Solution

Take a look at the javadoc of someThread.isInterrupted() method: ‘A thread interruption ignored because a thread was not alive at the time of the interrupt will be reflected by this method returning false.’ – I think it is the reason.

Also you should know, that you must listen for ‘interrupted status’ on yourself. By checking Thread.currentThread().isInterrupted(). And then may be throw new InterruptedException() on yourself.

Try the code below. The loop logic just for ‘waiting’ – you cannot use Thread.sleep(10_000) after thread interrupted.

    public static void main(String[] args) throws InterruptedException {
        System.out.println(Thread.currentThread().isInterrupted());
        Runnable target  new Runnable() {
            @Override
            public void run() {
                System.out.println("From thread-run(): " + Thread.currentThread().isInterrupted());
                Thread.currentThread().interrupt();
                System.out.println("After interruption");
                long sum  0;
                Random r  new Random();
                for (int i  0; i < Integer.MAX_VALUE; i++) {
                    sum + r.nextInt(3);
                }
                System.out.println(sum);
                try {
                    Thread.sleep(10_000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    System.out.println("InterruptedException");
                }
            }
        };
        Thread someThread  new Thread(target);
        System.out.println("Before start(): " + someThread.isInterrupted());
        someThread.start();
        Thread.sleep(1000);
        System.out.println("After start(): " + someThread.isInterrupted());
    }

Outputs:

false
Before start(): false
From thread-run(): false
After interruption
After start(): true
2147470801
java.lang.InterruptedException: sleep interrupted
    at java.base/java.lang.Thread.sleep(Native Method)
    at ru.eta2.Test$1.run(Test.java:20)
    at java.base/java.lang.Thread.run(Thread.java:834)
InterruptedException

Process finished with exit code 0

Answered By – Timur Efimov

Leave a Comment