zoukankan      html  css  js  c++  java
  • Java ExcutorService优雅关闭方式

    关闭时可使用如下代码

    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()在判断线程是否收到了中断信号

  • 相关阅读:
    POJ 3041 Asteroids 最小点覆盖 == 二分图的最大匹配
    POJ 3083 Children of the Candy Corn bfs和dfs
    POJ 2049 Finding Nemo bfs 建图很难。。
    POJ 2513 Colored Sticks 字典树、并查集、欧拉通路
    POJ 1013 Counterfeit Dollar 集合上的位运算
    POJ 2965 The Pilots Brothers' refrigerator 位运算枚举
    无聊拿socket写的100以内的加法考试。。。
    POJ 1753 Flip Game
    初学socket,c语言写的简单局域网聊天
    汇编语言 复习 第十一章 标志寄存器
  • 原文地址:https://www.cnblogs.com/migoo/p/11062154.html
Copyright © 2011-2022 走看看