这里写自定义目录标题
记录线程InterruptedException自动复位线程中断标志位
一般我们中断线程时习惯使用interrpt()去向线程发送中断请求。
今天发现了一个有意思的事情就是InterruptedException处理完后会复位中断标志位为false。
以下写了一个复现的Demo
public static void main(String[] args) {
Thread t = new Thread(()->{
try {
TimeUnit.SECONDS.sleep(1);
String threadName = Thread.currentThread().getName();
System.out.println(threadName + "准备中断=============");
} catch (InterruptedException e) {
System.out.println("==========处理中断异常===========");
}finally {
System.out.println("最终中断标志位: " + Thread.currentThread().isInterrupted());
}
}, "测试线程中断标志位interruptexception自动复位");
t.start();
t.interrupt();
System.out.println("===========执行中断==============");
}
结果
=执行中断====
处理中断异常=
最终中断标志位: false