Java – why is the value of “thread. Currentthread(). Isinterrupted()” false after capturing “interruptedexception”?

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 capturing "interruptedexception", why is the value of "thread. Currentthread(). Isinterrupted()" false?

Solution

From Javadoc for thread Sleep (called by timeunit.sleep):

I think the purpose of isinterrupted () is to allow you to detect whether the thread has been interrupted before calling the content that will throw interruptedexception If you encounter an interruptedexception, it is fair to assume that the thread is interrupted

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>