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

All Superinterfaces:
Future<V>
All Known Subinterfaces:
CheckedFuture<V,E>
All Known Implementing Classes:
AbstractCheckedFuture, AbstractListenableFuture, ForwardingListenableFuture, ListenableFutureTask, ValueFuture

@Beta
public interface ListenableFuture<V>
extends Future<V>

This interface defines a future that has listeners attached to it, which is useful for asynchronous workflows. Each listener has an associated executor, and is invoked using this executor once the Future's computation is complete. The listener will be executed even if it is added after the computation is complete.

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());
       }
     }
   }, exec);

Since:
1
Author:
Sven Mawson, Nishant Thakkar

Method Summary
 void addListener(Runnable listener, Executor exec)
          Adds a listener and executor to the ListenableFuture.
 
Methods inherited from interface java.util.concurrent.Future
cancel, get, get, isCancelled, isDone
 

Method Detail

addListener

void addListener(Runnable listener,
                 Executor exec)

Adds a listener and executor to the ListenableFuture. The listener will be passed to the executor for execution when the Future's computation is complete.

There is no guaranteed ordering of execution of listeners, they may get called in the order they were added and they may get called out of order, but any listener added through this method is guaranteed to be called once the computation is complete.

Parameters:
listener - the listener to run when the computation is complete.
exec - 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.