Interface ListenableFuture<V>

All Superinterfaces:
All Known Subinterfaces:
All Known Implementing Classes:
AbstractCheckedFuture, AbstractListenableFuture, ForwardingCheckedFuture, ForwardingCheckedFuture.SimpleForwardingCheckedFuture, ForwardingListenableFuture, ForwardingListenableFuture.SimpleForwardingListenableFuture, ListenableFutureTask, SettableFuture

public interface ListenableFuture<V>
extends Future<V>

A 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).


   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);

Sven Mawson, Nishant Thakkar

Method Summary
 void addListener(Runnable listener, Executor executor)
          Registers a listener to be run on the given executor.
Methods inherited from interface java.util.concurrent.Future
cancel, get, get, isCancelled, isDone

Method Detail


void addListener(Runnable listener,
                 Executor executor)
Registers a listener to be run on the given executor. The listener will run when the 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 complete
executor - the executor to run the listener in
NullPointerException - if the executor or listener was null
RejectedExecutionException - if we tried to execute the listener immediately but the executor rejected it.