Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to wait for list of `Future`s created using different `ExecutorServices`

Ok, so I know the first answer / comment here will be "use one ExecutorService and use invokeAll". However, there is a good reason (which I will not bore people with) for us keeping the thread pools separate.

So I have a list of thread pools (ExecutorServices) and what I need to do is invoke a different Callable on each thread pool using submit (no problem there). Now I have this collection of Future instances, each created on a seperate ExecutorService, and I want to wait for all of them to complete (and be able to provide a timeout at which any not done are cancelled).

Is there an existing class that will do this (wrap a list of Future instances and allow for a wait till all are done)? If not, suggestions on an efficient mechanism would be appreciated.

Was thinking of calling get with a timeout for each but have to do a calculation of the total time passed for each call.

I saw this post Wait Until Any of Future is Done but this extends Future instead of wrapping a list of them.

like image 558
John B Avatar asked Nov 01 '12 18:11

John B


1 Answers

Per Louis' comment, what I was looking for was Futures.successfulAsList

This allows me to wait for all to complete and then check for any futures that failed.

Guava RULES!

like image 80
John B Avatar answered Oct 14 '22 22:10

John B