@Beta public class ListenableFutureTask<V> extends FutureTask<V> implements ListenableFuture<V>
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.Constructor and Description |
---|
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. |
Modifier and Type | Method and Description |
---|---|
void |
addListener(Runnable listener,
Executor exec)
Registers a listener to be run on
the given executor.
|
protected void |
done() |
cancel, get, get, isCancelled, isDone, run, runAndReset, set, setException
public ListenableFutureTask(Callable<V> callable)
ListenableFutureTask
that will upon running, execute the
given Callable
.callable
- the callable taskNullPointerException
- if callable is nullpublic ListenableFutureTask(Runnable runnable, V result)
ListenableFutureTask
that will upon running, execute the
given Runnable
, and arrange that get
will return the
given result on successful completion.runnable
- the runnable taskresult
- 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)
NullPointerException
- if runnable is nullpublic void addListener(Runnable listener, Executor exec)
ListenableFuture
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.
addListener
in interface ListenableFuture<V>
listener
- the listener to run when the computation is completeexec
- the executor to run the listener inprotected void done()
done
in class FutureTask<V>