@Beta public interface ListenableFuture<V> extends Future<V>
Future that accepts completion listeners. Each listener has an
associated executor, and is invoked using this executor once the future's
computation is complete. If the computation has
already completed when the listener is added, the listener will execute
immediately.
Common ListenableFuture implementations include SettableFuture and the futures returned by a ListeningExecutorService (typically ListenableFutureTask
instances).
Usage:
final ListenableFuture<?> future = myService.async(myRequest);
future.addListener(new Runnable() {
public void run() {
System.out.println("Operation Complete.");
try {
System.out.println("Result: " + future.get());
} catch (Exception e) {
System.out.println("Error: " + e.message());
}
}
}, executor);| Modifier and Type | Method and Description |
|---|---|
void |
addListener(Runnable listener,
Executor executor)
Registers a listener to be run on
the given executor.
|
void addListener(Runnable listener, Executor executor)
Future's
computation is complete or, if the computation
is already complete, immediately.
There is no guaranteed ordering of execution of listeners, but any listener added through this method is guaranteed to be called once the computation is complete.
Listeners cannot throw checked exceptions and should not throw RuntimeException unless their executors are prepared to handle it.
Listeners that will execute in MoreExecutors.sameThreadExecutor()
should take special care, since they may run during the call to addListener or during the call that sets the future's value.
listener - the listener to run when the computation is completeexecutor - the executor to run the listener inNullPointerException - if the executor or listener was nullRejectedExecutionException - if we tried to execute the listener
immediately but the executor rejected it.Copyright © 2010-2012. All Rights Reserved.