com.google.common.util.concurrent
Interface ListenableFuture<V>

All Superinterfaces:
Future<V>
All Known Subinterfaces:
CheckedFuture<V,X>
All Known Implementing Classes:
AbstractCheckedFuture, AbstractListenableFuture, ForwardingCheckedFuture, ForwardingCheckedFuture.SimpleForwardingCheckedFuture, ForwardingListenableFuture, ForwardingListenableFuture.SimpleForwardingListenableFuture, ListenableFutureTask, SettableFuture

@Beta
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).

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

Since:
1
Author:
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

addListener

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.

Parameters:
listener - the listener to run when the computation is complete
executor - the executor to run the listener in
Throws:
NullPointerException - if the executor or listener was null
RejectedExecutionException - if we tried to execute the listener immediately but the executor rejected it.