阅读957 返回首页    go 京东网上商城


对interrupt、interrupted和isInterrupted的理解

1.interrupt
线程中断使用,Thread有stop() 不推荐使用,建议用中断的方法来实现
Thread.interrupt(),
2.interrupted
线程中断,并且清除中断状态(参考介绍interrupted的实现:https://blog.csdn.net/hj7jay/article/details/53462553)

public static boolean interrupted() {  
    return currentThread().isInterrupted(true);  
}
 * Tests if some Thread has been interrupted.  The interrupted state 
 * is reset or not based on the value of ClearInterrupted that is 
 * passed. 
 */  
private native boolean isInterrupted(boolean ClearInterrupted);  

3.isInterrupted
检查中断状态

4.InterruptedException
JDK中响应中断的方式是抛出异常,抛出InterruptedException时,会清除中断状态,参考:https://blog.csdn.net/hj7jay/article/details/53462553
这段引用:“
image

需要在catch InterruptedException中 执行 Thread.currentThread().interrupt(); 来恢复中断状态

5.无法被中断的处理方式介绍
参考:https://www.jianshu.com/p/f75b77bdf389
这篇介绍能被中断和无法被中断的处理方式介绍,可以看看无法被中断的场景以及参考的资料,同时感觉这位同学的这句话蛮好,“InterruptedException 是最常见的中断表现形式。所以如何处理 InterruptedException 便成为 Java 中断知识中的必修课。”

20171016

最后更新:2017-10-16 11:03:26

  上一篇:go  阿里云总裁胡晓明:未来和合作伙伴一起,服务1000万家企业
  下一篇:go  去中心化数字资产交易所系统