线程的Interrupted
来源:互联网 发布:深圳网络推广方法 编辑:程序博客网 时间:2024/06/05 11:37
If this thread is blocked in an invocation of the wait(), wait(long), or wait(long, int) methods of the Object class, or of the join(), join(long), join(long, int), sleep(long), or sleep(long, int), methods of this class, then its interrupt status will be cleared and it will receive an InterruptedException. 。
如果线程调用了wait ,jion, sleep方法而阻塞->>t.interrupt() -->将会抛出一个InterruptedException,异常抛出后会把刚才设置的中断状态标识也清除掉。
public class InterruptDemo {public static void main(String[] args) {Thread ex = new ExThread();ex.start();try {Thread.sleep(2000);} catch (InterruptedException e) {e.printStackTrace();}ex.interrupt();}}class ExThread extends Thread {@Overridepublic void run() {super.run();while (!Thread.currentThread().isInterrupted()) {System.out.println("ExThread 运行中:" + Thread.currentThread().isInterrupted());try {sleep(1000);} catch (InterruptedException e) {e.printStackTrace();System.out.println("InterruptedException异常:" + isInterrupted());//这里是false,因为异常抛出后,会清除中断状态的标识。所以While循环条件依旧满足,线程继续在那run..}}}}运行结果:
ExThread 运行中:false
ExThread 运行中:false
java.lang.InterruptedException: sleep interrupted
InterruptedException异常:false at java.lang.Thread.sleep(Native Method)
at com.thread.ExThread.run(InterruptDemo.java:28)
ExThread 运行中:false
ExThread 运行中:false
ExThread 运行中:false
ExThread 运行中:false
ExThread 运行中:false
如果我们在catch (InterruptedException e) 中再设置一下 this.interrupt();
public class InterruptDemo {public static void main(String[] args) {Thread ex = new ExThread();ex.start();try {Thread.sleep(2000);} catch (InterruptedException e) {e.printStackTrace();}ex.interrupt();}}class ExThread extends Thread {@Overridepublic void run() {super.run();while (!Thread.currentThread().isInterrupted()) {System.out.println("ExThread 运行中:" + Thread.currentThread().isInterrupted());try {sleep(1000);} catch (InterruptedException e) {e.printStackTrace();System.out.println("InterruptedException异常:" + isInterrupted());this.interrupt();System.out.println("是否中断:" + this.isInterrupted());}}}}
运行结果如下:
ExThread 运行中:falseExThread 运行中:falseInterruptedException异常:false是否中断:truejava.lang.InterruptedException: sleep interruptedat java.lang.Thread.sleep(Native Method)at com.thread.ExThread.run(InterruptDemo.java:28)
看吧,线程的whlie停止了。把上面的代码稍微改下:
public class InterruptDemo {public static void main(String[] args) {Thread ex = new ExThread();ex.start();try {Thread.sleep(2000);} catch (InterruptedException e) {e.printStackTrace();}ex.interrupt();}}class ExThread extends Thread {@Overridepublic void run() {super.run();while (!Thread.currentThread().isInterrupted()) {System.out.println("ExThread 运行中:" + Thread.currentThread().isInterrupted());try {sleep(1000);} catch (InterruptedException e) {e.printStackTrace();System.out.println("InterruptedException异常:" + isInterrupted());this.interrupt();<span style="color:#ff0000;">System.out.println("是否中断:" + Thread.interrupted());System.out.println("isInterrupted:" + this.isInterrupted());</span>}}}}
运行结果变成:
ExThread 运行中:false
ExThread 运行中:false
java.lang.InterruptedException: sleep interrupted
InterruptedException异常:false
是否中断:true
isInterrupted:false
ExThread 运行中:false
at java.lang.Thread.sleep(Native Method)
at com.thread.ExThread.run(InterruptDemo.java:28)
ExThread 运行中:false
ExThread 运行中:false
ExThread 运行中:false
ExThread 运行中:false
ExThread 运行中:false
ExThread 运行中:false
ExThread 运行中:false
为嘛呢? 因为Thread.interrupted()如果当前线程isAlive()不仅会返回当前线程的的中断状态值,并且会清除它,就是说返回true,后把它改为false.
- 线程的Interrupted
- 中断线程Interrupted的用处
- 【解惑】 中断线程Interrupted的用处
- 【解惑】 中断线程Interrupted的用处
- 【解惑】 中断线程Interrupted的用处
- 中断线程Interrupt与Interrupted的用处
- 【解惑】 中断线程Interrupted的用处
- 线程的终止stop与线程的中断interrupted
- 分析Java线程中断机制stop和interrupted的用法
- 线程玩具:打断(interrupted)
- 线程中断,interrupt(),interrupted,isInterrupted()
- interrupted()方法和线程停止
- Java线程的传说(1)——中断线程Interrupted的用处
- Java线程的传说(1)——中断线程Interrupted的用处
- java线程之中断线程Interrupted用法
- 关于asio的interrupted
- 多线程基础四(一)、停止不了的线程(interrupted、isInterrupted)
- interrupted()
- Ubuntu 安装 eclipse
- Undo internal<1>
- 半年总结篇
- dfs.datanode.max.xcievers
- windows下搭建SVN服务器及配置myeclipse SVN客户端
- 线程的Interrupted
- Eclipse for PHP Developers + xamp +xdebug
- poj-1850
- QTCreator3.0.0的BUG。。。竟然写铁 鞋两个字。。后面标点符号会显示不见(其实在)。。。
- HDU 4160 Dolls(DAG最小路径覆盖)
- Thread的run()方法与start()方法
- hibernate缓存:一级缓存和二级缓存
- 1基础-013:字符串
- swftools linux下安装