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

John B picture John B · Nov 1, 2012 · Viewed 10.9k times · Source

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.

Answer

John B picture John B · Nov 2, 2012

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!