as the title.
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
System.out.println(Thread.currentThread().isInterrupted()); //print false, who reset the interrupt?
}
}
});
thread.start();
TimeUnit.SECONDS.sleep(1);
thread.interrupt();
}
after catching “InterruptedException”, why “Thread.currentThread().isInterrupted()”‘s value is false?
From the Javadoc for
Thread.sleep(called byTimeUnit.sleep):I think the intention of
isInterrupted()is to allow you to detect whether a thread has been interrupted before calling something which would throwInterruptedException. If you’ve caughtInterruptedException, it’s fair to assume that the thread has been interrupted…