如何关闭ExecutorService?

时间:2012-05-08 18:17:50

标签: java multithreading executorservice shutdown

每当我致电shutdownNow()shutdown()时,它都不会关闭。我读了一些线索,其中说不能保证关闭 - 有人能为我提供一个很好的方法吗?

2 个答案:

答案 0 :(得分:81)

典型的模式是:

executorService.shutdownNow();
executorService.awaitTermination();

当调用shutdownNow时,执行程序将(通常)尝试中断它管理的线程。要使关闭正常,您需要捕获线程中的中断异常或检查中断状态。如果你不这样做,你的线程将永远运行,你的执行者永远无法关闭。这是因为Java中的线程中断是一个协作过程(即中断的代码在被要求停止时必须执行某些操作,而不是中断代码)。

例如,以下代码打印Exiting normally...。但是如果你注释掉行if (Thread.currentThread().isInterrupted()) break;,它将打印Still waiting...,因为执行程序中的线程仍在运行。

public static void main(String args[]) throws InterruptedException {
    ExecutorService executor = Executors.newFixedThreadPool(1);
    executor.submit(new Runnable() {

        @Override
        public void run() {
            while (true) {
                if (Thread.currentThread().isInterrupted()) break;
            }
        }
    });

    executor.shutdownNow();
    if (!executor.awaitTermination(100, TimeUnit.MICROSECONDS)) {
        System.out.println("Still waiting...");
        System.exit(0);
    }
    System.out.println("Exiting normally...");
}

或者,它可以用InterruptedException这样写:

public static void main(String args[]) throws InterruptedException {
    ExecutorService executor = Executors.newFixedThreadPool(1);
    executor.submit(new Runnable() {

        @Override
        public void run() {
            try {
                while (true) {Thread.sleep(10);}
            } catch (InterruptedException e) {
                //ok let's get out of here
            }
        }
    });

    executor.shutdownNow();
    if (!executor.awaitTermination(100, TimeUnit.MICROSECONDS)) {
        System.out.println("Still waiting...");
        System.exit(0);
    }
    System.out.println("Exiting normally...");
}

答案 1 :(得分:18)

最好的方法是我们在javadoc中实际拥有的是:

  

以下方法分两个阶段关闭 ExecutorService ,   首先通过调用shutdown拒绝传入的任务,然后调用   如果有必要,shutdownNow取消任何拖延的任务:

void shutdownAndAwaitTermination(ExecutorService pool) {
    pool.shutdown(); // Disable new tasks from being submitted
    try {
        // Wait a while for existing tasks to terminate
        if (!pool.awaitTermination(60, TimeUnit.SECONDS)) {
            pool.shutdownNow(); // Cancel currently executing tasks
            // Wait a while for tasks to respond to being cancelled
            if (!pool.awaitTermination(60, TimeUnit.SECONDS))
                System.err.println("Pool did not terminate");
        }
    } catch (InterruptedException ie) {
        // (Re-)Cancel if current thread also interrupted
        pool.shutdownNow();
        // Preserve interrupt status
        Thread.currentThread().interrupt();
    }
}