等待所有的任务完成

编程入门 行业动态 更新时间:2024-10-27 13:32:31
本文介绍了等待所有的任务完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一系列不同的任务使用相同的线程池。我想测量执行每个任务所需的时间,但是我需要等待任务中的每个任务(对不确定性的歧义)完成。

I have a series of different "tasks" to be done using the same thread pool. I want to measure the time it takes to perform each task, but for that I need to wait for every task in the "task" (sorry for ambiguity) to finish.

当只有一个任务我通常这样做:

When there's just one task I would normally do this:

ExecutorService e = Executors.newCachedThreadPool(); for (int i=0; i<100; ++i) e.submit(target); e.shutdown(); while (!e.isTerminated());

但是由于会有几个任务提交到池,我不能下来。所有与等待任务完成提示关闭请求后有关的方法。那么,如果我不想关闭它,但等待所有的线程完成,然后提交更多的任务,如果怎么办?

But since there will be several task submitted to the pool, I can't it down. All the methods that have something to do with waiting for the tasks to finish mention "after shutdown request". Well, what if I don't want to shut it down, but wait for all the threads to finish and then submit more tasks?

这是我想做的:

ExecutorService e = Executors.newCachedThreadPool(); for (int i=0; i<100; ++i) e.submit(target); // wait for all targets to finish for (int i=0; i<100; ++i) e.submit(target); // submit different tasks // wait... and so on

关闭池,然后再次使用 prestartAllCoreThreads 唤醒它,但后来我意识到这不是一个 ExecutorService 方法,但是 ThreadPoolExecutor 方法。这是一个解决方案吗?关闭它,等待,然后再次激活池?对我来说似乎有点丑陋。

I thought of shutting the pool down and then "waking it up" again using prestartAllCoreThreads, but then I realized this was not an ExecutorService method but a ThreadPoolExecutor method. Could this be a solution? Shutting it down, waiting, and then activating the pool again? Seems a bit ugly to me.

我也认为最自然的事情是使用 CyclicBarrier ,但它似乎太具体的方式这样做,而我认为这是最合乎逻辑的事情,能够使用任何 ExecutorService 为我的努力。

I also thought that the most natural thing to do was to use a CyclicBarrier, but it seems too a specific way of doing this, while I think it would be the most logical thing to be able to use any ExecutorService for what I'm trying to do.

有没有办法,我可以坚持 ExecutorService s,等待所有的任务完成? p>

Is there any way I could stick to ExecutorServices and wait for all the tasks to finish?

推荐答案

使用 CyclicBarrier

Use CyclicBarrier for the work you need like so :

// the optionalRunnable can collect the data gathered by the tasks CyclicBarrier b = new CyclicBarrier(numberOfTasks,optionalRunnable) Task yourTaks = new Task(...., b); // inside the run method call b.await() after the work is done; executor.submit(yourTaks);

也可以在主线程中调用await并实例化numTasks + 1的屏障。这样,您确定只有在完成处理当前批次后才向执行程序重新提交任务

Optionally , you can also call await in the main thread and instantiate the barrier to numTasks + 1 . That way you are sure you're resubmitting tasks to the executor only after it's done processing the current batch

更多推荐

等待所有的任务完成

本文发布于:2023-11-06 14:50:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1563990.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!