After catching "InterruptedException", why is "Thread.currentThread (.) IsInterrupted ()" false?
as a 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 is "Thread.currentThread (). isInterrupted ()" false?
source to share
From the Javadoc for Thread.sleep
(called TimeUnit.sleep
):
InterruptedException - if any thread interrupted the current thread. The interrupted status of the current thread is cleared on this exception.
I think the intent isInterrupted()
is for you to be able to determine if the thread has been interrupted before calling something that throws InterruptedException
. If you get caught InterruptedException
, it's fair to assume that the thread was interrupted ...
source to share