简体   繁体   English

ExecutorService关闭

[英]ExecutorService shutdown

I am confused about the javadoc of ExecutorService#shutdown method. 我对ExecutorService#shutdown方法的javadoc感到困惑。 Aren't these contradictory statements? 这些矛盾的陈述不是吗?

Initiates an orderly shutdown in which previously submitted tasks are executed, but no new tasks will be accepted. 启动有序关闭,其中先前提交的任务将被执行,但不会接受任何新任务。 This method does not wait for previously submitted tasks to complete execution. 此方法不会等待先前提交的任务完成执行。 Use awaitTermination to do that. 使用awaitTermination来做到这一点。

If it can orderly shutdown previously submitted tasks, then how can't it wait for them to complete execution? 如果它可以有序地关闭以前提交的任务,那么它怎么能等待它们完成执行呢?

It means that the method returns immediately in the thread that you call it in, but tasks that haven't yet been executed might still be running, in other threads. 这意味着该方法立即在您调用它的线程中返回,但是在其他线程中,尚未执行的任务可能仍在运行。

If you want your program to wait until the tasks that had been submitted previously have finished, you have to call awaitTermination after calling shutdown . 如果希望程序等到先前提交的任务完成,则必须在调用shutdown后调用awaitTermination

It means that the tasks will run to completion, but this method will return immediately, without waiting for that to happen. 这意味着任务将运行完成,但此方法将立即返回,而不会等待发生。

So, to cleanly shutdown your executor without killing any tasks, you would do: 因此,要干净地关闭执行程序而不执行任何任务,您可以:

executor.shutdown();
executor.awaitTermination(long timeout, TimeUnit unit);

Alternatively, if you just want to stop your executor as quickly as possible, use shutdownNow() . 或者,如果您只想尽快停止执行程序,请使用shutdownNow()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM