简体   繁体   English

等待线程池中所有工作程序完成的最佳方法是什么?

[英]What is the best way to wait for the completion of all workers in a thread pool?

Imagine I have following code: 假设我有以下代码:

final ExecutorService threadPool = Executors.newFixedThreadPool(
    NUMBER_OF_WORKERS);

for (int i=0; i < NUMBER_OF_WORKERS; i++)
{
  final Worker worker = new BirthWorker(...);
  threadPool.execute(worker);
}

Now I need a piece of code, which waits, until all workers have completed their work. 现在,我需要一段代码,等待所有工人完成工作。

Options I'm aware of: 我知道的选项:

  1. while (!threadPool.isTerminated()) {}
  2. Modify the code like that: 像这样修改代码:

    final List futures = new ArrayList(NUMBER_OF_WORKERS); 最终列表期货=新的ArrayList(NUMBER_OF_WORKERS); final ExecutorService threadPool = Executors.newFixedThreadPool(NUMBER_OF_WORKERS); 最终的ExecutorService threadPool = Executors.newFixedThreadPool(NUMBER_OF_WORKERS);

    for (int i=0; i < NUMBER_OF_WORKERS; i++) { final Worker worker = new Worker(...); for(int i = 0; i <NUMBER_OF_WORKERS; i ++){final worker worker = new Worker(...); futures.add(threadPool.submit(worker)); futures.add(threadPool.submit(工人)); } }

    for (final Future future : futures) { future.get(); 对于(最终Future Future:Futures){future.get(); } }

    // When we arrive here, all workers are guaranteed to have completed their work. //当我们到达这里时,保证所有工人都已完成工作。

What is the best practice to wait for the completion of all workers? 等待所有工人完成工作的最佳实践是什么?

I would suggest you use CountDownLatch (assuming this is one time activity) where in your constructor, you can specify how many threads you want to wait for and you share that instance accross the threads and you wait on all the threads to complete using await api (using timeout or complete blocking) and your thread's calling countdown api when they are done. 我建议您使用CountDownLatch (假设这是一次活动),在构造函数中,您可以指定要等待的线程数,并在线程中共享该实例,然后使用await api等待所有线程完成(使用超时或完全阻止)以及线程完成后调用线程的countdown api。

Another option would be, to call join method in thread to wait for their completion if you have access to each and every thread that you wish to complete. 另一个选择是,如果您有权访问要完成的​​每个线程,则在线程中调用join方法以等待其完成。

I would use ThreadPoolExecutor.invokeAll(Collection<? extends Callable<T>> tasks) 我将使用ThreadPoolExecutor.invokeAll(Collection<? extends Callable<T>> tasks)

API: Executes the given tasks, returning a list of Futures holding their status and results when all complete API:执行给定的任务,并在所有任务完成后返回保存其状态和结果的期货列表

CountDownLatch,as stated above, would do the work well, just keep in mind that you want to shut down the executur after your done: 如上所述,CountDownLatch可以很好地完成工作,只需记住您要在完成后关闭执行程序:

final ExecutorService threadPool = Executors.newFixedThreadPool(
NUMBER_OF_WORKERS);

for (int i=0; i < NUMBER_OF_WORKERS; i++)
{
  final Worker worker = new BirthWorker(...);
  threadPool.execute(worker);
}
threadPool.shutdown();

unless you shut it down, threadPool.isTerminated will stay false, even when all the workers are done. 除非您将其关闭,否则即使所有工作程序都已完成,threadPool.isTerminated也将保持为假。

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

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