我们该如何正确的中断一个正在执行的线程??

网络 通信技术
作者个人研发的在高并发场景下,提供的简单、稳定、可扩展的延迟消息队列框架,具有精准的定时任务和延迟队列处理功能。

[[358852]]

作者个人研发的在高并发场景下,提供的简单、稳定、可扩展的延迟消息队列框架,具有精准的定时任务和延迟队列处理功能。自开源半年多以来,已成功为十几家中小型企业提供了精准定时调度方案,经受住了生产环境的考验。为使更多童鞋受益,现给出开源框架地址:https://github.com/sunshinelyz/mykit-delay

 写在前面

当我们在调用Java对象的wait()方法或者线程的sleep()方法时,需要捕获并处理InterruptedException异常。如果我们对InterruptedException异常处理不当,则会发生我们意想不到的后果!今天,我们就以一个案例的形式,来为大家详细介绍下为何中断执行的线程不起作用。

程序案例

例如,下面的程序代码,InterruptedTask类实现了Runnable接口,在run()方法中,获取当前线程的句柄,并在while(true)循环中,通过isInterrupted()方法来检测当前线程是否被中断,如果当前线程被中断就退出while(true)循环,同时,在while(true)循环中,还有一行Thread.sleep(100)代码,并捕获了InterruptedException异常。整个代码如下所示。

  1. package io.binghe.concurrent.lab08; 
  2.  
  3. /** 
  4.  * @author binghe 
  5.  * @version 1.0.0 
  6.  * @description 线程测试中断 
  7.  */ 
  8. public class InterruptedTask implements Runnable{ 
  9.  
  10.     @Override 
  11.     public void run() { 
  12.  
  13.         Thread currentThread = Thread.currentThread(); 
  14.         while (true){ 
  15.             if(currentThread.isInterrupted()){ 
  16.                 break; 
  17.             } 
  18.  
  19.             try { 
  20.                 Thread.sleep(100); 
  21.             } catch (InterruptedException e) { 
  22.                 e.printStackTrace(); 
  23.             } 
  24.         } 
  25.     } 

上述代码的本意是通过isInterrupted()方法检查线程是否被中断了,如果中断了就退出while循环。其他线程通过调用执行线程的interrupt()方法来中断执行线程,此时会设置执行线程的中断标志位,从而使currentThread.isInterrupted()返回true,这样就能够退出while循环。

这看上去没啥问题啊!但真的是这样吗?我们创建一个InterruptedTest类用于测试,代码如下所示。

  1. package io.binghe.concurrent.lab08; 
  2.  
  3. /** 
  4.  * @author binghe 
  5.  * @version 1.0.0 
  6.  * @description 测试线程中断 
  7.  */ 
  8. public class InterruptedTest { 
  9.     public static void main(String[] args){ 
  10.         InterruptedTask interruptedTask = new InterruptedTask(); 
  11.         Thread interruptedThread = new Thread(interruptedTask); 
  12.         interruptedThread.start(); 
  13.         try { 
  14.             Thread.sleep(1000); 
  15.         } catch (InterruptedException e) { 
  16.             e.printStackTrace(); 
  17.         } 
  18.         interruptedThread.interrupt(); 
  19.     } 

我们运行main方法,如下所示。

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

问题分析

上述代码明明调用了线程的interrupt()方法来中断线程,但是却并没有起到啥作用。原因是线程的run()方法在执行的时候,大部分时间都是阻塞在sleep(100)上,当其他线程通过调用执行线程的interrupt()方法来中断执行线程时,大概率的会触发InterruptedException异常,在触发InterruptedException异常的同时,JVM会同时把线程的中断标志位清除,所以,这个时候在run()方法中判断的currentThread.isInterrupted()会返回false,也就不会退出当前while循环了。

既然问题分析清楚了,那如何中断线程并退出程序呢?

问题解决

正确的处理方式应该是在InterruptedTask类中的run()方法中的while(true)循环中捕获异常之后重新设置中断标志位,所以,正确的InterruptedTask类的代码如下所示。

  1. package io.binghe.concurrent.lab08; 
  2.  
  3. /** 
  4.  * @author binghe 
  5.  * @version 1.0.0 
  6.  * @description 中断线程测试 
  7.  */ 
  8. public class InterruptedTask implements Runnable{ 
  9.  
  10.     @Override 
  11.     public void run() { 
  12.  
  13.         Thread currentThread = Thread.currentThread(); 
  14.         while (true){ 
  15.             if(currentThread.isInterrupted()){ 
  16.                 break; 
  17.             } 
  18.  
  19.             try { 
  20.                 Thread.sleep(100); 
  21.             } catch (InterruptedException e) { 
  22.                 e.printStackTrace(); 
  23.                 currentThread.interrupt(); 
  24.             } 
  25.         } 
  26.     } 

可以看到,我们在捕获InterruptedException异常的catch代码块中新增了一行代码。

  1. currentThread.interrupt(); 

这就使得我们捕获到InterruptedException异常后,能够重新设置线程的中断标志位,从而中断当前执行的线程。

我们再次运行InterruptedTest类的main方法,如下所示。

总结

处理InterruptedException异常时要小心,如果在调用执行线程的interrupt()方法中断执行线程时,抛出了InterruptedException异常,则在触发InterruptedException异常的同时,JVM会同时把执行线程的中断标志位清除,此时调用执行线程的isInterrupted()方法时,会返回false。此时,正确的处理方式是在执行线程的run()方法中捕获到InterruptedException异常,并重新设置中断标志位(也就是在捕获InterruptedException异常的catch代码块中,重新调用当前线程的interrupt()方法)。

本文转载自微信公众号「冰河技术」,可以通过以下二维码关注。转载本文请联系冰河技术公众号。

 

责任编辑:武晓燕 来源: 冰河技术
相关推荐

2018-06-28 08:41:37

2014-07-02 10:03:42

App推广渠道

2016-09-30 09:49:05

2020-06-01 11:01:28

智慧城市物联网技术

2021-11-03 12:34:41

黑客网络钓鱼攻击

2020-06-20 14:15:53

信息安全数据技术

2021-11-15 10:35:46

Python线程代码

2018-03-07 11:00:27

2021-10-27 06:49:34

线程池Core函数

2010-09-09 21:34:06

2022-03-09 09:43:01

工具类线程项目

2021-02-03 10:34:35

多云云安全CISO

2024-03-25 12:30:05

AI模型

2023-02-01 16:36:31

GNOMEChatGPTLinux

2010-02-23 15:52:14

Python编辑器

2014-11-28 09:45:35

程序员

2010-04-01 09:18:38

云计算

2014-08-07 10:52:18

GitHub代码托管

2021-04-29 15:55:51

编程IT程序员

2015-12-02 11:23:38

DockerUber容器服务
点赞
收藏

51CTO技术栈公众号