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.
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.
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.
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.
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.
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.
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