java线程中的interrupt,isInterrupt,interrupted方法
来源:互联网 发布:高铁买票软件 编辑:程序博客网 时间:2024/05/17 23:52
在java的线程Thread类中有三个方法,比较容易混淆,在这里解释一下
(1)interrupt:置线程的中断状态
(2)isInterrupt:线程是否中断
(3)interrupted:返回线程的上次的中断状态,并清除中断状态
举个例子:
一般来说,阻塞函数,如:Thread.sleep、Thread.join、Object.wait、LockSupport.park等在检查到线程的中断状态时,会抛出InterruptedException,同时会清除线程的中断状态
对于InterruptedException的处理,可以有两种情况:
(1)外层代码可以处理这个异常,直接抛出这个异常即可
(2)如果不能抛出这个异常,比如在run()方法内,因为在得到这个异常的同时,线程的中断状态已经被清除了,需要保留线程的中断状态,则需要调用Thread.currentThread().interrupt()
另外,Thread.interrupted()在jdk库的源代码中比较常用,因为它既可以得到上一次线程的中断标志值,又可以同时清除线程的中断标志,一举两得,但同时也有坏处,就是这个函数有清除中断状态的副作用,不容易理解
(1)interrupt:置线程的中断状态
(2)isInterrupt:线程是否中断
(3)interrupted:返回线程的上次的中断状态,并清除中断状态
举个例子:
- 用法:
- 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库的源代码中比较常用,因为它既可以得到上一次线程的中断标志值,又可以同时清除线程的中断标志,一举两得,但同时也有坏处,就是这个函数有清除中断状态的副作用,不容易理解
0 0
- 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、interrupted、isInterrupt方法
- java线程中的interrupt,isInterrupt,interrupted方法
- java线程中的interrupt,isInterrupt,interrupted方法以及如何终止线程(一)
- java线程中的interrupt,isInterrupt,interrupted方法以及如何终止线程(二)
- Java thread的Interrupt, isInterrupt, interrupted
- 关于Java thread的interrupt, isInterrupt, interrupted
- 关于Java thread的interrupt, isInterrupt, interrupted
- interrupt、isinterrupt、interrupted的理解和使用
- Java中的线程中断:interrupt()、interrupted()和isInterrupted
- Java interrupted()方法和isInterrupt方法的区别
- 区分线程中的 interrupt() interrupted() isInterrupted()
- 小训练题解
- TCP Socket协议定义
- fscanf()函数详解
- HDU 1533 费用流
- nyoj - 947(Max Xor)字典树
- java线程中的interrupt,isInterrupt,interrupted方法
- 我为什么要学习Linux?
- CString类
- HDU 1171 Big Event in HDU
- 关于arc与非arc的混合编程
- 集合类的retainCount
- RabbitMQ (消息队列)专题学习01 RabbitMQ部署
- C. Inna and Dima 深度搜索
- 动态代理