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

java.lang.Object
  extended by java.util.concurrent.FutureTask<V>
      extended by com.google.common.util.concurrent.ListenableFutureTask<V>
All Implemented Interfaces:
ListenableFuture<V>, Runnable, Future<V>, RunnableFuture<V>

@Beta
public class ListenableFutureTask<V>
extends FutureTask<V>
implements ListenableFuture<V>

A FutureTask that also implements the ListenableFuture interface. Subclasses must make sure to call super.done() if they also override the done() method, otherwise the listeners will not be called.

Since:
1
Author:
Sven Mawson

Constructor Summary
ListenableFutureTask(Callable<V> callable)
          Creates a ListenableFutureTask that will upon running, execute the given Callable.
ListenableFutureTask(Runnable runnable, V result)
          Creates a ListenableFutureTask that will upon running, execute the given Runnable, and arrange that get will return the given result on successful completion.
 
Method Summary
 void addListener(Runnable listener, Executor exec)
          Adds a listener and executor to the ListenableFuture.
protected  void done()
           
 
Methods inherited from class java.util.concurrent.FutureTask
cancel, get, get, isCancelled, isDone, run, runAndReset, 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

ListenableFutureTask

public ListenableFutureTask(Callable<V> callable)
Creates a ListenableFutureTask that will upon running, execute the given Callable.

Parameters:
callable - the callable task
Throws:
NullPointerException - if callable is null

ListenableFutureTask

public ListenableFutureTask(Runnable runnable,
                            V result)
Creates a ListenableFutureTask that will upon running, execute the given Runnable, and arrange that get will return the given result on successful completion.

Parameters:
runnable - the runnable task
result - the result to return on successful completion. If you don't need a particular result, consider using constructions of the form: ListenableFuture<?> f = new ListenableFutureTask<Object>(runnable, null)
Throws:
NullPointerException - if runnable is null
Method Detail

addListener

public void addListener(Runnable listener,
                        Executor exec)
Description copied from interface: ListenableFuture

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.

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 FutureTask<V>