In the below code snippet, we are calling interrupt twice, after first interrupt, exception is thrown but why the status of thread.isInterrupted is false? Do we always need to call another interrupt to set the status?
public void run() {
try {
System.out.println("I am too sleepy... Let me sleep for an hour.");
Thread.sleep(1000 * 60 * 60);
} catch (InterruptedException ie) {
System.out.println("The interrupt flag is cleard : " + Thread.interrupted() + " " + Thread.currentThread().isInterrupted());
Thread.currentThread().interrupt();
System.out.println("Oh someone woke me up ! ");
System.out.println("The interrupt flag is set now : " + Thread.currentThread().isInterrupted() + " " + Thread.interrupted());
}
}
});
sleepyThread.start();
System.out.println("About to wake up the sleepy thread ...");
sleepyThread.interrupt();
System.out.println("Woke up sleepy thread ...");
sleepyThread.join();
Course: Java Multithreading for Senior Engineering Interviews - Learn Interactively
Lesson: Interrupting Threads - Java Multithreading for Senior Engineering Interviews