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.