Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different behaviour when calling thread.isInterrupted and printing the result

I am bit confused with the behaviour of thread.isInterrupted in the program below.

public class ThreadPractice {

    public static void main(String args[]) throws InterruptedException {

        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    System.out.println("Starting thread..." + Thread.currentThread().getName());
                    Thread.sleep(10000);
                    System.out.println("Waking up");
                }catch(InterruptedException e){
                    System.out.println("Thread is interrupted!!!");
                    Thread.currentThread().interrupt();
                }
            }
        });

        t.start();
        Thread.sleep(2000);
        t.interrupt();
        //System.out.println(!t.isInterrupted());
        while(!t.isInterrupted()){
            System.out.println("Current thread not interrupted!!!");
        } 
    }
}

When executing the above program as such, it prints,

Starting thread...Thread-0
Thread is interrupted!!!

But when I uncomment the System.out statement to print the interrupt status, it runs into an infinite loop printing "Current thread not interrupted"

I am not able to figure out exactly what difference System.out statement makes.

like image 844
Kumar V Avatar asked Dec 15 '14 18:12

Kumar V


People also ask

What is the difference between the interrupted () and isInterrupted () method in Java?

interrupted() method is a static method of class thread checks the current thread and clear the interruption "flag". i.e. a second call to interrupted() will return false. isInterrupted() method is an instance method; it reports the status of the thread on which it is invoked. it does not clear the interruption flag.

What are different ways of thread interruption in Java?

This means interruption of a thread is caused by any other thread calling the interrupt() method. void interrupt() - Interrupts the thread. static boolean interrupted() - Tests whether the current thread has been interrupted. boolean isInterrupted() - Tests whether the thread has been interrupted.

What is the difference between Isinterrupt method and interrupted method?

This method simply returns a flag that is set by the interrupt() method. The main difference between these two methods is that the interrupted() method resets the value of the flag to false. The isInterrupted() method does not change the value of the flag.

What happens when a thread is interrupted?

Threads can be interrupted, and when a thread is interrupted, it will throw InterruptedException. In the next sections, we'll see InterruptedException in detail and learn how to respond to it.


1 Answers

Note that I don't always get an infinite loop.

I suspect it is due to the interleaving of operations. It helps to also check if the thread is alive:

System.out.println("Current thread not interrupted!!! Alive? " + t.isAlive());

If the thread is not alive, its interrupted status is false.

I get an output like:

Starting thread...Thread-0
Thread is interrupted!!!
Current thread not interrupted!!! Alive? true
Current thread not interrupted!!! Alive? false
[infinite loop]

I guess the first loop sees the thread not interrupted because the InterruptedException has removed the flag - then you reset the flag with interrupt() in the run() method and the thread can finish and is not alive any more.
The next loop check sees it not alive and you start an infinite loop.


An interesting variation can be obtained by adding:

System.out.println("Exiting Thread!!!");

At the end of the run() method - it provides a delay long enough for the loop condition to be checked between the time the interrupted flag is reset and the time the thread dies.

like image 101
assylias Avatar answered Oct 23 '22 05:10

assylias