Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><code>ExecutorService.invokeAll()</code> returns list of <code>Futures&lt;?&gt;</code> then you can iterate through this list and execute <code>future.get()</code> that will throw an exception if it occurs during computation or a result if no error occurs.</p> <p>Here the example:</p> <pre><code>class SecondFailTask implements Callable&lt;Integer&gt; { private static volatile int counter = 0; @Override public Integer call() throws Exception { counter++; if (counter == 2){ throw new RuntimeException("Fail"); } else { return counter; } } public static void main(String[] args) throws Exception{ ExecutorService e = Executors.newSingleThreadExecutor(); List&lt;SecondFailTask&gt; tasks = Arrays.asList(new SecondFailTask(),new SecondFailTask(),new SecondFailTask()); List&lt;Future&lt;Integer&gt;&gt; futures = e.invokeAll(tasks); for (Future&lt;Integer&gt; future : futures){ try { System.out.println("Counter is " + future.get()); }catch (ExecutionException ex){ System.out.println(ex.getCause()); } } } </code></pre> <p>Output is:</p> <pre><code>Counter is 1 java.lang.RuntimeException: Fail Counter is 3 </code></pre> <p>I'm not sure that there is a way to grab the results of all tasks at once as far I understood <code>onSuccess()</code> or <code>onFailure()</code> executes after each task computation and in such way collecting results would be trick(with some global variable). I would prefer to use <code>ExecutorService.invokeAll()</code> but I'm not a guava expert and I might miss something.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload