Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Timeout while waiting for a batch of Futures to complete?

I have a set of Futures created by submitting Callables to an Executor. Pseudo code:

for all tasks
  futures.add(executor.submit(new callable(task)))

Now I'd like to get all futures waiting at most n seconds until all complete. I know I can call Future#get(timeout) but if I call that sequentially for all my futures in a loop the timouts start adding up. Pseudo code:

for all futures
  future.get(timeout)

get blocks with a timeout until the result is ready. Therefore, if the first completes just before timeout and the second also completes just before timeout and so on the entire execution time is number of futures * timeout at most instead of timeout.

Hence, I'm looking for a method that accepts a list of Futures and a timeout, runs all in parallel and then returns a collection of future results. Any ideas?

like image 526
Marcel Stör Avatar asked Jul 02 '13 19:07

Marcel Stör


1 Answers

You can use ExecutorService.invokeAll:

Executes the given tasks, returning a list of Futures holding their status and results when all complete or the timeout expires, whichever happens first. Future.isDone() is true for each element of the returned list. Upon return, tasks that have not completed are cancelled. 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.


If you already have Futures that you need to monitor and can't use invokeAll, you can simply measure the timeout yourself. Pseudo code:

long endTime = System.currentTimeMillis() + timeoutMS;
for(f : futures)
    f.get(Math.max(0, endTime - System.currentTimeMillis()), TimeUnit.MILLISECONDS);

This way you give each future at most the duration that is left until you reach your timeout.

like image 78
Petr Avatar answered Sep 18 '22 06:09

Petr