com.google.common.util.concurrent
Class AbstractListenableFuture<V>

java.lang.Object
  extended by com.google.common.util.concurrent.AbstractFuture<V>
      extended by com.google.common.util.concurrent.AbstractListenableFuture<V>
All Implemented Interfaces:
ListenableFuture<V>, Future<V>
Direct Known Subclasses:
SettableFuture

@Beta
public abstract class AbstractListenableFuture<V>
extends AbstractFuture<V>
implements ListenableFuture<V>

An abstract base implementation of the listener support provided by ListenableFuture. This class uses an ExecutionList to guarantee that all registered listeners will be executed. Listener/Executor pairs are stored in the execution list and executed in the order in which they were added, but because of thread scheduling issues there is no guarantee that the JVM will execute them in order. In addition, listeners added after the task is complete will be executed immediately, even if some previously added listeners have not yet been executed.

This class uses the AbstractFuture class to implement the ListenableFuture interface and simply delegates the addListener(Runnable, Executor) and done() methods to it.

Since:
1
Author:
Sven Mawson

Constructor Summary
AbstractListenableFuture()
           
 
Method Summary
 void addListener(Runnable listener, Executor exec)
          Registers a listener to be run on the given executor.
protected  void done()
           
 
Methods inherited from class com.google.common.util.concurrent.AbstractFuture
cancel, cancel, get, get, isCancelled, isDone, set, setException
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface java.util.concurrent.Future
cancel, get, get, isCancelled, isDone
 

Constructor Detail

AbstractListenableFuture

public AbstractListenableFuture()
Method Detail

addListener

public void addListener(Runnable listener,
                        Executor exec)
Description copied from interface: ListenableFuture
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.

Specified by:
addListener in interface ListenableFuture<V>
Parameters:
listener - the listener to run when the computation is complete
exec - the executor to run the listener in

done

protected void done()
Overrides:
done in class AbstractFuture<V>