当咱们在调用Java对象的wait()方法或者线程的sleep()方法时,须要捕获并处理InterruptedException异常。若是咱们对InterruptedException异常处理不当,则会发生咱们意想不到的后果!今天,咱们就以一个案例的形式,来为你们详细介绍下为什么中断执行的线程不起做用。java
文章已收录到:https://github.com/sunshinelyz/technology-binghe 和 https://gitee.com/binghe001/technology-binghegit
例如,下面的程序代码,InterruptedTask类实现了Runnable接口,在run()方法中,获取当前线程的句柄,并在while(true)循环中,经过isInterrupted()方法来检测当前线程是否被中断,若是当前线程被中断就退出while(true)循环,同时,在while(true)循环中,还有一行Thread.sleep(100)代码,并捕获了InterruptedException异常。整个代码以下所示。程序员
package io.binghe.concurrent.lab08; /** * @author binghe * @version 1.0.0 * @description 线程测试中断 */ public class InterruptedTask implements Runnable{ @Override public void run() { Thread currentThread = Thread.currentThread(); while (true){ if(currentThread.isInterrupted()){ break; } try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } } }
上述代码的本意是经过isInterrupted()方法检查线程是否被中断了,若是中断了就退出while循环。其余线程经过调用执行线程的interrupt()方法来中断执行线程,此时会设置执行线程的中断标志位,从而使currentThread.isInterrupted()返回true,这样就可以退出while循环。github
这看上去没啥问题啊!但真的是这样吗?咱们建立一个InterruptedTest类用于测试,代码以下所示。面试
package io.binghe.concurrent.lab08; /** * @author binghe * @version 1.0.0 * @description 测试线程中断 */ public class InterruptedTest { public static void main(String[] args){ InterruptedTask interruptedTask = new InterruptedTask(); Thread interruptedThread = new Thread(interruptedTask); interruptedThread.start(); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } interruptedThread.interrupt(); } }
咱们运行main方法,以下所示。微信
这居然跟咱们想象的不同!不同!不同!这是为何呢?ide
上述代码明明调用了线程的interrupt()方法来中断线程,可是却并无起到啥做用。缘由是线程的run()方法在执行的时候,大部分时间都是阻塞在sleep(100)上,当其余线程经过调用执行线程的interrupt()方法来中断执行线程时,大几率的会触发InterruptedException异常,在触发InterruptedException异常的同时,JVM会同时把线程的中断标志位清除,因此,这个时候在run()方法中判断的currentThread.isInterrupted()会返回false,也就不会退出当前while循环了。学习
既然问题分析清除了,那如何中断线程并退出程序呢?测试
正确的处理方式应该是在InterruptedTask类中的run()方法中的while(true)循环中捕获异常以后从新设置中断标志位,因此,正确的InterruptedTask类的代码以下所示。线程
package io.binghe.concurrent.lab08; /** * @author binghe * @version 1.0.0 * @description 中断线程测试 */ public class InterruptedTask implements Runnable{ @Override public void run() { Thread currentThread = Thread.currentThread(); while (true){ if(currentThread.isInterrupted()){ break; } try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); currentThread.interrupt(); } } } }
能够看到,咱们在捕获InterruptedException异常的catch代码块中新增了一行代码。
currentThread.interrupt();
这就使得咱们捕获到InterruptedException异常后,可以从新设置线程的中断标志位,从而中断当前执行的线程。
咱们再次运行InterruptedTest类的main方法,以下所示。
处理InterruptedException异常时要当心,若是在调用执行线程的interrupt()方法中断执行线程时,抛出了InterruptedException异常,则在触发InterruptedException异常的同时,JVM会同时把执行线程的中断标志位清除,此时调用执行线程的isInterrupted()方法时,会返回false。此时,正确的处理方式是在执行线程的run()方法中捕获到InterruptedException异常,并从新设置中断标志位(也就是在捕获InterruptedException异常的catch代码块中,从新调用当前线程的interrupt()方法)。
好了,今天就到这儿吧,我是冰河,咱们下期见~~
微信搜一搜【冰河技术】微信公众号,关注这个有深度的程序员,天天阅读超硬核技术干货,公众号内回复【PDF】有我准备的一线大厂面试资料和我原创的超硬核PDF技术文档,以及我为你们精心准备的多套简历模板(不断更新中),但愿你们都能找到心仪的工做,学习是一条时而郁郁寡欢,时而开怀大笑的路,加油。若是你经过努力成功进入到了心仪的公司,必定不要懈怠放松,职场成长和新技术学习同样,不进则退。若是有幸咱们江湖再见!
另外,我开源的各个PDF,后续我都会持续更新和维护,感谢你们长期以来对冰河的支持!!