Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>With <code>ListenableFuture</code>, you can submit any number of tasks and then pass the <code>ListenableFuture</code>s from those tasks to <a href="http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/util/concurrent/Futures.html#allAsList%28com.google.common.util.concurrent.ListenableFuture...%29"><code>Futures.allAsList</code></a>, getting another <code>ListenableFuture</code> that will complete when <em>all</em> of the tasks have completed. There's also <a href="http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/util/concurrent/Futures.html#successfulAsList%28com.google.common.util.concurrent.ListenableFuture...%29"><code>Futures.successfulAsList</code></a>, which succeeds even if some of the tasks fail and gives you a <code>null</code> result for each failed task.</p> <p>You can then either block the current thread to wait for those results (using the normal <code>Future.get()</code>) or you can add a listener/callback to be called when they've completed if you don't want/need to block the current thread.</p> <p>Example:</p> <pre><code>ListeningExecutorService executor = ... List&lt;Callable&lt;Foo&gt;&gt; tasks = ... List&lt;ListenableFuture&lt;Foo&gt;&gt; futures = Lists.newArrayList(); for (Callable&lt;Foo&gt; task : tasks) { futures.add(executor.submit(task)); } final ListenableFuture&lt;List&lt;Foo&gt;&gt; resultsFuture = Futures.allAsList(futures); // block until all tasks are done List&lt;Foo&gt; results = resultsFuture.get(); // or add a callback to get called when the tasks complete Futures.addCallback(resultsFuture, new FutureCallback&lt;List&lt;Foo&gt;&gt;() { @Override public void onSuccess(List&lt;Foo&gt; results) { // ... } @Override public void onFailure(Throwable throwable) { // ... } }, someExecutor); </code></pre>
 

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