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)
          Registers a listener to be run on the given executor.
protected  void done()
          Protected method invoked when this task transitions to state isDone (whether normally or via cancellation).
 
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
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()
Description copied from class: java.util.concurrent.FutureTask
Protected method invoked when this task transitions to state isDone (whether normally or via cancellation). The default implementation does nothing. Subclasses may override this method to invoke completion callbacks or perform bookkeeping. Note that you can query status inside the implementation of this method to determine whether this task has been cancelled.

Overrides:
done in class FutureTask<V>