java 在关闭 Executor 之前等待所有线程完成

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12193513/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 07:58:35  来源:igfitidea点击:

Waiting for all the threads to finish before shutting down the Executors

javawaitexecutorservice

提问by Kraken

Here is my code snippet.

这是我的代码片段。

ExecutorService executor = Executors.newFixedThreadPool(ThreadPoolSize);
while(conditionTrue)
{
ClassImplementingRunnable c = new ClassImplementingRunnable();
executor.submit(c);
}

Now after this is do

现在这样做之后

executor.shutdown();

What i want to achieve here is that i want to wait for all the threads in the threadpool to have finished the execution and then i want to shutdown the executor.

我想在这里实现的是,我想等待线程池中的所有线程完成执行,然后我想关闭执行程序。

But i guess this is not what is happening here. The main thread seems to be executing shutdown and it just shuts down everything.

但我想这不是这里发生的事情。主线程似乎正在执行关闭,它只是关闭了一切。

Before when my threadpool size was 2, i did the following and it seemed to work.

在我的线程池大小为 2 之前,我执行了以下操作并且它似乎有效。

ClassImplementingRunnable c1 = new ClassImplementingRunnable();
executor.submit(c1);
ClassImplementingRunnable c2 = new ClassImplementingRunnable();
executor.submit(c2);
Future f1 = executor.submit(c1);
Future f2 = executor.submit(c2);
while(!f1.done || !f2.done)
{}
executor.submit();

How do i do this for a greater number of threads in the threadpool? Thanks.

我如何为线程池中的更多线程执行此操作?谢谢。

回答by assylias

You generally use the following idiom:

您通常使用以下习语:

executor.shutdown();
executor.awaitTermination(Integer.MAX_VALUE, TimeUnit.SECONDS);
  • shutdownjust says that the executor won't accept new jobs.
  • awaitTerminationwaits until all the tasks that have already been submitted finish what they are doing (or until the timeout is reached - which won't happen with Integer.MAX_VALUE - you might want to use a lower value).
  • shutdown只是说执行人不会接受新工作。
  • awaitTermination等待所有已经提交的任务完成他们正在做的事情(或者直到超时 - 这不会发生在 Integer.MAX_VALUE - 你可能想要使用一个较低的值)。

回答by Marko Topolnik

shutdownis a "soft" order and won't abruptly shut down the executor. It will do just what you want: refuse any new tasks, wait for all submitted tasks to complete, and then shut down.

shutdown是一个“软”命令,不会突然关闭执行程序。它会做你想做的事:拒绝任何新任务,等待所有提交的任务完成,然后关闭。

Add awaitTerminationto your code in order to block until the executor service is shut down.

添加awaitTermination到您的代码中以阻止直到执行程序服务关闭。

In the documentationthere's also this code that covers all the angles:

文档中还有这段代码涵盖了所有的角度:

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();
   }
 }

回答by Peter Lawrey

What i want to achieve here is that i want to wait for all the threads in the threadpool to have finished the execution and then i want to shutdown the executor.

我想在这里实现的是,我想等待线程池中的所有线程完成执行,然后我想关闭执行程序。

That is what executor.shutdown();does. If you want to shutdown immediately you need to use shutdownNow()

这就是executor.shutdown();它的作用。如果你想立即关机,你需要使用shutdownNow()