Java ExcutorService优雅关闭方式解析
作者:migoo 时间:2023-11-25 19:21:48
关闭时可使用如下代码
public static void waitUntilTerminate(final ExecutorService executorService, final int timeout) {
try {
executorService.shutdown();
if (!executorService.awaitTermination(timeout, TimeUnit.SECONDS)) { //超时后直接关闭
executorService.shutdownNow();
}
} catch (InterruptedException e) { //awaitTermination 出现中断异常也将触发关闭
executorService.shutdownNow();
}
}
但是实际使用中,可能会出现即使使用了shutdownNow方法,还是无法终止线程的问题,那是因为你的线程无法被中断
shutdownNow方法简单理解就是给在运行的线程发一个中断信号,如果你的线程忽略这个信号,那就无法停下来
举个例子来说明这个问题
public class ShutDownUtilsTest {
private ExecutorService executorService;
@Before
public void init() {
executorService = Executors.newFixedThreadPool(1);
}
@Test
public void shutDownOKTest() {
ShutDownUtils.waitUntilTerminate(executorService, 1);
CommonUtils.sleep(1); //等待线程处理中断
Assert.assertTrue(executorService.isTerminated());
}
@Test
public void shutDownNowFailTest() {
executorService.execute(this::canNotStopThread);
ShutDownUtils.waitUntilTerminate(executorService, 0);
CommonUtils.sleep(1); //等待线程处理中断
Assert.assertFalse(executorService.isTerminated());
}
@Test
public void shutDownNowOKTest() {
executorService.execute(this::stopThread);
ShutDownUtils.waitUntilTerminate(executorService, 0);
CommonUtils.sleep(1); //等待线程处理中断
Assert.assertTrue(executorService.isTerminated());
}
private void canNotStopThread() {
for (long i = 0; i < Long.MAX_VALUE; i++) {
}
}
private void stopThread() {
for (long i = 0; i < Long.MAX_VALUE && !Thread.currentThread().isInterrupted(); i++) {
}
}
}
从上面的测试用例可以看到canNotStopThread无法被shutDownNow终止
然而stopThread可以被正常终止,因为通过Thread.currentThread().isInterrupted()在判断线程是否收到了中断信号
来源:https://www.cnblogs.com/migoo/p/11062154.html
标签:Java,Excutor,Service,关闭
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
java简单工厂模式实例及讲解
2021-09-19 14:08:39
Unity3D实现批量下载图片功能
2021-07-03 13:00:14
![](https://img.aspxhome.com/file/2023/3/106503_0s.png)
android gradle如何修改生成的apk名字
2023-03-09 23:38:56
![](https://img.aspxhome.com/file/2023/2/138502_0s.jpg)
SpringMVC上传图片与访问
2023-12-06 17:54:31
基于WPF实现简单的文件夹比较工具
2023-11-15 16:08:34
![](https://img.aspxhome.com/file/2023/6/74836_0s.png)
SpringBoot 如何实现异步编程
2023-04-15 13:45:43
java实现MD5加密的方法小结
2022-02-26 20:01:47
基于Java实现的图的广度优先遍历算法
2021-06-02 06:51:20
Java客户端调用.NET的WebService实例
2023-11-03 17:22:00
![](https://img.aspxhome.com/file/2023/8/58988_0s.png)
详解5种Java中常见限流算法
2023-02-01 16:14:31
Spring Security OAuth2认证授权示例详解
2022-09-11 19:45:47
![](https://img.aspxhome.com/file/2023/1/72281_0s.png)
Android超详细讲解组件ScrollView的使用
2022-08-05 09:55:18
![](https://img.aspxhome.com/file/2023/9/94989_0s.png)
深入探究Spring底层核心原理
2023-03-05 08:32:16
C#对list列表进行随机排序的方法
2021-10-31 20:00:17
在Java中判断两个Long对象是否相等
2022-09-01 11:22:10
详解Android中Activity运行时屏幕方向与显示方式
2022-02-12 03:51:06
![](https://img.aspxhome.com/file/2023/8/137748_0s.gif)
Android编程实现自定义进度条颜色的方法
2023-07-24 07:40:58
![](https://img.aspxhome.com/file/2023/8/138038_0s.jpg)
使用JavaWeb webSocket实现简易的点对点聊天功能实例代码
2023-10-29 00:14:17
Android编程中Tween动画和Frame动画实例分析
2023-12-03 06:31:56
Java线程安全问题小结_动力节点Java学院整理
2021-09-25 21:01:43