阻塞直到 ExecutorService 完成

发布于 2024-11-16 04:17:46 字数 679 浏览 5 评论 0 原文

可能的重复:
ExecutorService,如何等待所有任务完成
Java ExecutorService:awaitTermination所有递归创建的任务

如何阻止当前线程直到 ExecutorService 完成其所有任务?

executor.execute(task1);
executor.execute(task2);
executor.execute(task3);
executor.execute(task4);
executor.execute(task5);
// ...now I want to block until all tasks have finished executing...
System.out.println("done!")

Possible Duplicates:
ExecutorService, how to wait for all tasks to finish
Java ExecutorService: awaitTermination of all recursively created tasks

Is there a way to block the current thread until an ExecutorService has finished all its tasks?

executor.execute(task1);
executor.execute(task2);
executor.execute(task3);
executor.execute(task4);
executor.execute(task5);
// ...now I want to block until all tasks have finished executing...
System.out.println("done!")

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

岁吢 2024-11-23 04:17:46

对于所有任务,将它们放入可调用列表中,然后

ExecutorService e = ...

e.invokeAll(callables);

根据 javadoc

对它们调用所有

执行给定的任务,返回
保持其状态的期货列表
以及全部完成后的结果。
Future.isDone() 对于每个都是 true
返回列表的元素。笔记
一个已完成的任务可以有
正常终止或通过
抛出异常。结果
如果给定,则此方法未定义
集合被修改,而这
操作正在进行中。

因此线程将等待直到所有任务都完成

For all your tasks, put them into a List callables then invokeAll on them

ExecutorService e = ...

e.invokeAll(callables);

Per javadocs

Executes the given tasks, returning a
list of Futures holding their status
and results when all complete.
Future.isDone() is true for each
element of the returned list. Note
that a completed task could have
terminated either normally or by
throwing an exception. The results of
this method are undefined if the given
collection is modified while this
operation is in progress.

Thus the thread will wait until all tasks haven been completed

-黛色若梦 2024-11-23 04:17:46

您可以使用 ThreadPoolExecutor 池大小设置为 System.getRuntime().availableProcessors()Java 6Runtime.getRuntime().availableProcessors()Java 8.execute() 全部您要执行的任务,然后调用 tpe.shutdown() 并在 while(!tpe.termerated()) 中等待 { /* 等待所有任务完成 */}< /code> 阻止完成所有提交的任务。 其中 tpe 是对 ThreadPoolExecutor 实例的引用。

或者,如果更合适,请使用 ExecutorCompletionService A CompletionService 使用提供的 Executor 来执行任务。此类安排提交的任务在完成后放置在可使用 take 访问的队列中。该类足够轻量,适合在处理任务组时短暂使用。

You can use a ThreadPoolExecutor with a pool size set to System.getRuntime().availableProcessors()Java 6 or Runtime.getRuntime().availableProcessors()Java 8 and .execute() it all the tasks you want to execute, then call tpe.shutdown() and then wait in a while(!tpe.terminated()) { /* waiting for all tasks to complete */} which blocks for all the submitted tasks to complete. where tpe is a reference to your ThreadPoolExecutor instance.

Or if it is more appropriate use an ExecutorCompletionService A CompletionService that uses a supplied Executor to execute tasks. This class arranges that submitted tasks are, upon completion, placed on a queue accessible using take. The class is lightweight enough to be suitable for transient use when processing groups of tasks.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文