超实用!!!学习dubbo如何优雅关闭线程池

2,647 阅读2分钟

基本方法学习

线程池是我们经常使用的工具,也是面试必问的知识点,那么如何优雅的关闭线程池那?

线程池相信大家都使用过,但是使用完成之后如何关闭线程池大家真的未必真的会使用。有人可能会说调用shutdown或者shutdownNow就可以了,真的有那么简单吗?如果你关闭线程池的姿势不正确,最严重的情况会导致线程一直存在系统中。

  • shutDown:通知线程池启动有序关闭,执行线程池之前已经提交的任务,但是不再接受新的任务。调用shutDown后再提交任务将会抛出RejectedExecutionException异常。

  • shutDownNow:尝试立即停止所有已经提交的任务,并会返回正在等待执行(未执行)的任务列表。shutDownNow通过向线程池中的线程发送一个中断请求而中止线程,如果线程池中运行了会抛出InterruptedException的程序,将会抛出一个InterruptedException。如过这个线程不能响应中断那么可能永远无法被终止。

  • isTerminated:所有的任务都被关闭返回true,否则返回false。只有调用了shutDown或者shutDownNow,isTerminated才可能为true。

  • awaitTermination(long timeout, TimeUnit unit) throws InterruptedException:阻塞当前线程直到

    • 所有任务执行完毕
    • 或者超时
    • 或者当前线程被中断

    如果所有任务都关闭,则返回true,否则返回false。

优雅关闭线程池的正确姿势

  • step1:执行shutdown方法,等待所有任务执行完毕并拒绝新任务的提交。
  • step2:执行awaitTermination(long timeout,TimeUnit unit),指定超时时间,判断是是否已经关闭所有任务,防止线程永远无法关闭。
  • step3:如果step2返回fasle,或者被中断。调用shutDownNow方法立即关闭线程池所有任务。

dubbo关闭线程池工具类学习

public class ExecutorUtil {
    private static final Logger logger = LoggerFactory.getLogger(ExecutorUtil.class);
    private static final ThreadPoolExecutor shutdownExecutor = new ThreadPoolExecutor(0, 1,
            0L, TimeUnit.MILLISECONDS,
            new LinkedBlockingQueue<Runnable>(100),
            new NamedThreadFactory("Close-ExecutorService-Timer", true));

    public static boolean isTerminated(Executor executor) {
        if (executor instanceof ExecutorService) {
            if (((ExecutorService) executor).isTerminated()) {
                return true;
            }
        }
        return false;
    }

    /**
     * Use the shutdown pattern from:
     *  https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ExecutorService.html
     * @param executor the Executor to shutdown
     * @param timeout the timeout in milliseconds before termination
     */
    public static void gracefulShutdown(Executor executor, int timeout) {
        if (!(executor instanceof ExecutorService) || isTerminated(executor)) {
            return;
        }
        final ExecutorService es = (ExecutorService) executor;
        try {
            // Disable new tasks from being submitted
            es.shutdown();
        } catch (SecurityException ex2) {
            return;
        } catch (NullPointerException ex2) {
            return;
        }
        try {
            // Wait a while for existing tasks to terminate
            if (!es.awaitTermination(timeout, TimeUnit.MILLISECONDS)) {
                es.shutdownNow();
            }
        } catch (InterruptedException ex) {
            es.shutdownNow();
            Thread.currentThread().interrupt();
        }
        if (!isTerminated(es)) {
            newThreadToCloseExecutor(es);
        }
    }

    public static void shutdownNow(Executor executor, final int timeout) {
        if (!(executor instanceof ExecutorService) || isTerminated(executor)) {
            return;
        }
        final ExecutorService es = (ExecutorService) executor;
        try {
            es.shutdownNow();
        } catch (SecurityException ex2) {
            return;
        } catch (NullPointerException ex2) {
            return;
        }
        try {
            es.awaitTermination(timeout, TimeUnit.MILLISECONDS);
        } catch (InterruptedException ex) {
            Thread.currentThread().interrupt();
        }
        if (!isTerminated(es)) {
            newThreadToCloseExecutor(es);
        }
    }

    private static void newThreadToCloseExecutor(final ExecutorService es) {
        if (!isTerminated(es)) {
            shutdownExecutor.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        for (int i = 0; i < 1000; i++) {
                            es.shutdownNow();
                            if (es.awaitTermination(10, TimeUnit.MILLISECONDS)) {
                                break;
                            }
                        }
                    } catch (InterruptedException ex) {
                        Thread.currentThread().interrupt();
                    } catch (Throwable e) {
                        logger.warn(e.getMessage(), e);
                    }
                }
            });
        }
    }
}