如果发生关机,executorService是否确保等待终止?

时间:2019-05-08 19:30:49

标签: java executorservice

我有以下代码:

public class AbcThreadPool {

    private final AbcThreadPoolExecutor executor;

    public InventoryAvailabilityThreadPool(final AbcRunnableFactory factory,
                                           final Integer poolSize) {
        executor = new AbcThreadPoolExecutor(factory, poolSize);

        for (int i = 0; i < poolSize; ++i) {
            executor.execute(factory.createRunnable());
        }
    }

    private static class AbcThreadPoolExecutor extends ThreadPoolExecutor {

        private final AbcRunnableFactory factory;

        public AbcThreadPoolExecutor(final AbcRunnableFactory factory,
                                                       final int poolSize) {
            super(poolSize, poolSize, 0, TimeUnit.SECONDS, new LinkedBlockingQueue<>());
            this.factory = factory;
            allowCoreThreadTimeOut(false);
        }
    }


    @Override
    protected void afterExecute(Runnable r, Throwable t) {
        if (!isShutdown() && !Thread.currentThread().isInterrupted()) {
            execute(factory.createRunnable());
        }
    }
}

在此代码中,它将处理以下情况:如果通过Ctrl + C指示关闭进程,它将在杀死线程之前等待线程终止?或者我需要添加一个shutdownHook来调用

executorService.awaitTermination

程序退出之前?

0 个答案:

没有答案