Java ExcutorService优雅关闭方式解析
关闭时可使用如下代码
publicstaticvoidwaitUntilTerminate(finalExecutorServiceexecutorService,finalinttimeout){
try{
executorService.shutdown();
if(!executorService.awaitTermination(timeout,TimeUnit.SECONDS)){//超时后直接关闭
executorService.shutdownNow();
}
}catch(InterruptedExceptione){//awaitTermination出现中断异常也将触发关闭
executorService.shutdownNow();
}
}
但是实际使用中,可能会出现即使使用了shutdownNow方法,还是无法终止线程的问题,那是因为你的线程无法被中断
shutdownNow方法简单理解就是给在运行的线程发一个中断信号,如果你的线程忽略这个信号,那就无法停下来
举个例子来说明这个问题
publicclassShutDownUtilsTest{
privateExecutorServiceexecutorService;
@Before
publicvoidinit(){
executorService=Executors.newFixedThreadPool(1);
}
@Test
publicvoidshutDownOKTest(){
ShutDownUtils.waitUntilTerminate(executorService,1);
CommonUtils.sleep(1);//等待线程处理中断
Assert.assertTrue(executorService.isTerminated());
}
@Test
publicvoidshutDownNowFailTest(){
executorService.execute(this::canNotStopThread);
ShutDownUtils.waitUntilTerminate(executorService,0);
CommonUtils.sleep(1);//等待线程处理中断
Assert.assertFalse(executorService.isTerminated());
}
@Test
publicvoidshutDownNowOKTest(){
executorService.execute(this::stopThread);
ShutDownUtils.waitUntilTerminate(executorService,0);
CommonUtils.sleep(1);//等待线程处理中断
Assert.assertTrue(executorService.isTerminated());
}
privatevoidcanNotStopThread(){
for(longi=0;i<Long.MAX_VALUE;i++){
}
}
privatevoidstopThread(){
for(longi=0;i<Long.MAX_VALUE&&!Thread.currentThread().isInterrupted();i++){
}
}
}
从上面的测试用例可以看到canNotStopThread无法被shutDownNow终止
然而stopThread可以被正常终止,因为通过Thread.currentThread().isInterrupted()在判断线程是否收到了中断信号
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
热门推荐
6 保研的祝福语简短
10 年轻20岁祝福语简短
11 朋友结婚祝福语信息简短
12 女孩婚礼贺卡祝福语简短
13 30段点歌简短祝福语
14 虎年春节祝福语图文简短
15 写给后妈祝福语大全简短
16 简短回复生日祝福语
17 校长送毕业祝福语简短
18 毕业立体贺卡祝福语简短