对interrupt、interrupted和isInterrupted的理解

简介: 抛出中断状态的处理,和中断的语法关键字解释

1.interrupt

线程中断使用,Thread有stop() 不推荐使用,建议用中断的方法来实现
Thread.interrupt(),

2.interrupted

线程中断,并且清除中断状态(参考介绍interrupted的实现:http://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时,会清除中断状态,参考:http://blog.csdn.net/hj7jay/article/details/53462553
这段引用:“
![image](https://yqfile.alicdn.com/1ae2093c42d36cd8ea79ebd53ff3f2805b8b881e.png)
”

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

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


20171016
目录
相关文章
|
4月前
线程中断方法详解interrupt
线程中断方法详解interrupt
39 0
|
Java
线程中断方法interrupt、isInterrupted、interrupted方法
线程中断方法interrupt、isInterrupted、interrupted方法
85 0
线程中断方法interrupt、isInterrupted、interrupted方法
|
Java
简述Thread的interrupt()、interrupted()及isInterrupted()的区别
简述Thread的interrupt()、interrupted()及isInterrupted()的区别
114 0
简述Thread的interrupt()、interrupted()及isInterrupted()的区别
|
安全 Java
浅析wait与synchronized
在Java语言中,Object作为顶级的父类中有一个wait()方法,我们都知道wait()跟notify()作为Java中的线程通信机制,但是你有没有想过:为什么wait方法是在Object中?它为什么不是在Thread中?
104 0
浅析wait与synchronized
wait和notify 为什么要在synchronized代码块中
wait和notify 为什么要在synchronized代码块中
wait和notify 为什么要在synchronized代码块中
|
Java 调度 开发者
线程 - 你真的懂 wait、notify、notifyAll 吗?
线程 - 你真的懂 wait、notify、notifyAll 吗?
97 0
java中interrupt,interrupted和isInterrupted的区别
java中interrupt,interrupted和isInterrupted的区别
|
Java API
interrupted()和isInterrupted()
interrupted()和isInterrupted()
109 0
|
API
interrupted()和isInterrupted()详述
interrupted()和isInterrupted()详述
107 0
interrupted()和isInterrupted()详述