Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run a set of tasks in parallel and wait until all of them are finished with java concurrency utils?

I have N tasks, I want them to be processed in parallel with N threads. I want to wait until all tasks are finished, store results, and then run next N tasks (and so on in a loop).

Which abstractions from java util concurrency can help me here?

I looked at ExecutorService.invokeAll(), but it returns a list of futures, so I should iterate all of them in a loop until all of them are done.

I thought, there should be more straightforward way to calculate a set of tasks.

like image 864
Roman Avatar asked Dec 29 '25 03:12

Roman


1 Answers

ExecutorService.invokeAll() is probably the most straightforward way of doing it.

You don't iterate over any of them to await completion. invokeAll only returns when all of them are completed. So by the time you can iterate over them, get will return immediately.

like image 117
John Vint Avatar answered Dec 30 '25 16:12

John Vint



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!