Thread.interrupted & Thread.currentThread().isInterrupted();

源码:

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

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容