写点什么

InterruptedException 异常会对并发编程产生哪些影响?

作者:冰河
  • 2022-11-25
    四川
  • 本文字数:2061 字

    阅读完需:约 7 分钟

InterruptedException异常会对并发编程产生哪些影响?

写在前面

InterruptedException 异常可能没你想的那么简单!

前言

当我们在调用 Java 对象的 wait()方法或者线程的 sleep()方法时,需要捕获并处理 InterruptedException 异常。如果我们对 InterruptedException 异常处理不当,则会发生我们意想不到的后果!

程序案例

例如,下面的程序代码,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 循环。


这看上去没啥问题啊!**但真的是这样吗?**我们创建一个 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 方法,如下所示。



这竟然跟我们想象的不一样!不一样!不一样!这是为什么呢?

问题分析

上述代码明明调用了线程的 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()方法)。

写在最后

如果觉得文章对你有点帮助,请微信搜索并关注「 冰河技术 」微信公众号,跟冰河学习高并发编程技术。


最后,附上并发编程需要掌握的核心技能知识图,祝大家在学习并发编程时,少走弯路。



发布于: 刚刚阅读数: 4
用户头像

冰河

关注

公众号:冰河技术,专注写硬核技术专栏。 2020-05-29 加入

互联网资深技术专家,《深入理解高并发编程:核心原理与案例实战》《深入理解分布式事务:原理与实战》《海量数据处理与大数据技术实战》《MySQL技术大全》作者,mykit-data框架作者。【冰河技术】作者。

评论

发布
暂无评论
InterruptedException异常会对并发编程产生哪些影响?_并发编程_冰河_InfoQ写作社区