源码:
Thread.interrupted
public static boolean interrupted() {
return currentThread().isInterrupted(true);
}
Thread.currentThread().isInterrupted():
public boolean isInterrupted() {
return isInterrupted(false);
}
最后调用底层,从参数名可以看出,interrupted静态方法会擦出线程的中断标志,而Thread.currentThread().isInterrupted()不会执行擦除。
private native boolean isInterrupted(boolean ClearInterrupted);
代码验证:
//输出当前线程的中断标志
System.out.println(Thread.currentThread().isInterrupted());
//标记中断
Thread.currentThread().interrupt();
//输出当前线程的中断标志
System.out.println(Thread.currentThread().isInterrupted());
//输出当前线程中断标志并擦除
System.out.println(Thread.interrupted());
//再次输出当前线程的中断标志
System.out.println(Thread.currentThread().isInterrupted());
结果输出:
false
true
true
false
网友评论