java线程中的interrupt,isInterrupt,interrupted方法
2017-04-07 14:00
691 查看
interrupt方法并不是强制终止线程,它只能设置线程的interrupted状态
---------------------------我是分割线---------------------------------
java线程中的interrupt,isInterrupt,interrupted方法
在Java的线程Thread类中有三个方法,比较容易混淆,在这里解释一下(1)interrupt:置线程的中断状态
(2)isInterrupt:线程是否中断
(3)interrupted:返回线程的上次的中断状态,并清除中断状态
举个例子:
[java]
view plain
copy
print?
用法:
class MyThread extends Thread {
......
......
public void run() {
try {
while(!Thread.currentThread().isInterrupted()) {
//当达到队列容量时,在这里会阻塞
//put的内部会调用LockSupport.park()这个是用来阻塞线程的方法
//当其他线程,调用此线程的interrupt()方法时,会设置一个中断标志
//LockSupport.part()中检测到这个中断标志,会抛出InterruptedException,并清除线程的中断标志
//因此在异常段调用Thread.currentThread().isInterrupted()返回为false
ArrayBlockingQueue.put(somevalue);
}
} catch (InterruptedException e) {
//由于阻塞库函数,如:Object.wait,Thread.sleep除了抛出异常外,还会清除线程中断状态,因此可能在这里要保留线程的中断状态
Thread.currentThread().interrupt();
}
}
public void cancel() {
interrupt();
}
}
外部调用
MyThread thread = new MyThread();
thread.start();
......
thread.cancel();
thread.isInterrupted();
用法: class MyThread extends Thread { ...... ...... public void run() { try { while(!Thread.currentThread().isInterrupted()) { //当达到队列容量时,在这里会阻塞 //put的内部会调用LockSupport.park()这个是用来阻塞线程的方法 //当其他线程,调用此线程的interrupt()方法时,会设置一个中断标志 //LockSupport.part()中检测到这个中断标志,会抛出InterruptedException,并清除线程的中断标志 //因此在异常段调用Thread.currentThread().isInterrupted()返回为false ArrayBlockingQueue.put(somevalue); } } catch (InterruptedException e) { //由于阻塞库函数,如:Object.wait,Thread.sleep除了抛出异常外,还会清除线程中断状态,因此可能在这里要保留线程的中断状态 Thread.currentThread().interrupt(); } } public void cancel() { interrupt(); } } 外部调用 MyThread thread = new MyThread(); thread.start(); ...... thread.cancel(); thread.isInterrupted();
一般来说,阻塞函数,如:Thread.sleep、Thread.join、Object.wait、LockSupport.park等在检查到线程的中断状态时,会抛出InterruptedException,同时会清除线程的中断状态
对于InterruptedException的处理,可以有两种情况:
(1)外层代码可以处理这个异常,直接抛出这个异常即可
(2)如果不能抛出这个异常,比如在run()方法内,因为在得到这个异常的同时,线程的中断状态已经被清除了,需要保留线程的中断状态,则需要调用Thread.currentThread().interrupt()
另外,Thread.interrupted()在jdk库的源代码中比较常用,因为它既可以得到上一次线程的中断标志值,又可以同时清除线程的中断标志,一举两得,但同时也有坏处,就是这个函数有清除中断状态的副作用,不容易理解
相关文章推荐
- java线程中的interrupt,isInterrupt,interrupted方法
- java线程中的interrupt、interrupted、isInterrupt方法
- 转:java线程中的interrupt,isInterrupt,interrupted方法
- java线程中的interrupt,isInterrupt,interrupted方法
- java线程中的interrupt,isInterrupt,interrupted方法
- java线程中的interrupt,isInterrupt,interrupted方法
- java线程中的interrupt,isInterrupt,interrupted方法
- java线程中的interrupt,isInterrupt,interrupted方法
- java线程中的interrupt,isInterrupt,interrupted方法
- java线程中的interrupt,isInterrupt,interrupted方法
- java线程中的interrupt,isInterrupt,interrupted方法以及如何终止线程(二)
- java线程中的interrupt,isInterrupt,interrupted方法以及如何终止线程(一)
- thinkinjava21.4.4----java中interrupt和interrupted方法的使用和线程退出的时机
- java线程之sleep、interrupt方法
- 停止Java线程,小心interrupt()方法
- 关闭超时的java线程 --利用join和interrupt方法
- 关于java线程的interrupt方法的使用总结
- 停止Java线程,小心interrupt()方法
- 停止Java线程,小心interrupt()方法
- Java线程--interrupt join yield setDaemon常用方法的使用