子线程interrupted为什么在main线程中获取不到这个状态

子线程interrupted,在catchException之后,重新设置了此线程为已中断的状态,为什么在代码的最后一行获取的状态仍然是false?


public class TheadStatusTest {

    public static void main(String[] args) throws InterruptedException {

        Thread s = new Thread(()->{
            try {
                System.out.println(Thread.currentThread().getName());
                Thread.sleep(Integer.MAX_VALUE);
            } catch (InterruptedException e) {
                //抛出异常后,线程的标示位已经clear成false了
                // 这里返回false
                System.out.println("INTERRUPTED ..." + Thread.currentThread().isInterrupted());
//                System.out.println("INTERRUPTED ..." + Thread.interrupted()); //
                e.printStackTrace();

                //重新设置标示位
                Thread.currentThread().interrupt();
                // 这里返回ture
                System.out.println("INTERRUPTED 2 ..." + Thread.currentThread().isInterrupted());
            }
        }, "Abc-thread-");
        s.start();

        //设置线程标示位为true //
        s.interrupt();
        Thread.sleep(1000);
        System.out.println("===>"+ s.isInterrupted()); //这个状态是在main.sleep之后获取的,为什么是false?
    }
}
阅读 1.3k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题