// interrupted 最终会调用 isInterrupted,但传入参数为 true,表示要擦除中断标识
public static boolean interrupted() {
return currentThread().isInterrupted(true);
}
// isInterrupted 最终会调用 isInterrupted,但传入参数为 false,表示不擦除中断标识
public boolean isInterrupted() {
return isInterrupted(false);
}
private native boolean isInterrupted(boolean ClearInterrupted);
如果一线程被中断后,再去执行可中断方法,会怎么样呢?我们不妨测试一下。
package concurrent;
import java.util.concurrent.TimeUnit;
public class ThreadInterrpt1 {
public static void main(String[] args) {
// 判断当前线程是否中断
System.out.println("Main thread is interruputed?" + Thread.interrupted());
// 中断当前线程
Thread.currentThread().interrupt();
// 中断当前线程
System.out.println("Main thread is interruputed?" + Thread.currentThread().isInterrupted());
// 当前线程执行可中断方法
try {
TimeUnit.MINUTES.sleep(1);
} catch (InterruptedException e) {
System.out.println("捕获中断信号");
// 中断后,擦除中断标识
System.out.println("Main thread is interruputed?" + Thread.currentThread().isInterrupted());
}
}
}
Main thread is interruputed?false
Main thread is interruputed?true
捕获中断信号
Main thread is interruputed?false
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/chengqiuming/article/details/123099994
内容来源于网络,如有侵权,请联系作者删除!