public abstract class AbstractFuture<V> extends Object implements ListenableFuture<V>
ListenableFuture
interface. This
class is preferable to FutureTask
for two
reasons: It implements ListenableFuture
, and it does not implement
Runnable
. (If you want a Runnable
implementation of ListenableFuture
, create a ListenableFutureTask
, or submit your
tasks to a ListeningExecutorService
.)
This class implements all methods in ListenableFuture
.
Subclasses should provide a way to set the result of the computation through
the protected methods set(Object)
and
setException(Throwable)
. Subclasses may also override interruptTask()
, which will be invoked automatically if a call to cancel(true)
succeeds in canceling the future.
AbstractFuture
uses an AbstractQueuedSynchronizer
to deal
with concurrency issues and guarantee thread safety.
The state changing methods all return a boolean indicating success or failure in changing the future's state. Valid states are running, completed, failed, or cancelled.
This class uses an ExecutionList
to guarantee that all registered
listeners will be executed, either when the future finishes or, for listeners
that are added after the future completes, immediately.
Runnable
-Executor
pairs are stored in the execution list but
are not necessarily executed in the order in which they were added. (If a
listener is added after the Future is complete, it will be executed
immediately, even if earlier listeners have not been executed. Additionally,
executors need not guarantee FIFO execution, or different listeners may run
in different executors.)
Modifier | Constructor and Description |
---|---|
protected |
AbstractFuture()
Constructor for use by subclasses.
|
Modifier and Type | Method and Description |
---|---|
void |
addListener(Runnable listener,
Executor exec)
Registers a listener to be run on
the given executor.
|
boolean |
cancel(boolean mayInterruptIfRunning)
Attempts to cancel execution of this task.
|
V |
get()
Waits if necessary for the computation to complete, and then
retrieves its result.
|
V |
get(long timeout,
TimeUnit unit)
Waits if necessary for at most the given time for the computation
to complete, and then retrieves its result, if available.
|
protected void |
interruptTask()
Subclasses can override this method to implement interruption of the
future's computation.
|
boolean |
isCancelled()
Returns true if this task was cancelled before it completed
normally.
|
boolean |
isDone()
Returns true if this task completed.
|
protected boolean |
set(V value)
Subclasses should invoke this method to set the result of the computation
to
value . |
protected boolean |
setException(Throwable throwable)
Subclasses should invoke this method to set the result of the computation
to an error,
throwable . |
protected boolean |
wasInterrupted()
Returns true if this future was cancelled with
mayInterruptIfRunning set to true . |
protected AbstractFuture()
public V get(long timeout, TimeUnit unit) throws InterruptedException, TimeoutException, ExecutionException
The default AbstractFuture
implementation throws InterruptedException
if the current thread is interrupted before or during
the call, even if the value is already available.
get
in interface Future<V>
timeout
- the maximum time to waitunit
- the time unit of the timeout argumentInterruptedException
- if the current thread was interrupted before
or during the call (optional but recommended).CancellationException
- if the computation was cancelledTimeoutException
- if the wait timed outExecutionException
- if the computation threw an
exceptionpublic V get() throws InterruptedException, ExecutionException
The default AbstractFuture
implementation throws InterruptedException
if the current thread is interrupted before or during
the call, even if the value is already available.
get
in interface Future<V>
InterruptedException
- if the current thread was interrupted before
or during the call (optional but recommended).CancellationException
- if the computation was cancelledExecutionException
- if the computation threw an
exceptionpublic boolean isDone()
java.util.concurrent.Future
public boolean isCancelled()
java.util.concurrent.Future
isCancelled
in interface Future<V>
public boolean cancel(boolean mayInterruptIfRunning)
java.util.concurrent.Future
After this method returns, subsequent calls to Future.isDone()
will
always return true. Subsequent calls to Future.isCancelled()
will always return true if this method returned true.
cancel
in interface Future<V>
mayInterruptIfRunning
- true if the thread executing this
task should be interrupted; otherwise, in-progress tasks are allowed
to completeprotected void interruptTask()
cancel(true)
.
The default implementation does nothing.
protected final boolean wasInterrupted()
mayInterruptIfRunning
set to true
.public void addListener(Runnable listener, Executor exec)
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.
Exceptions thrown by a listener will be propagated up to the executor.
Any exception thrown during Executor.execute
(e.g., a RejectedExecutionException
or an exception thrown by inline execution) will be caught and
logged.
Note: For fast, lightweight listeners that would be safe to execute in
any thread, consider MoreExecutors.sameThreadExecutor()
. For heavier
listeners, sameThreadExecutor()
carries some caveats. For
example, the listener may run on an unpredictable or undesirable thread:
Future
is done at the time addListener
is
called, addListener
will execute the listener inline.
Future
is not yet done, addListener
will
schedule the listener to be run by the thread that completes this Future
, which may be an internal system thread such as an RPC network
thread.
Also note that, regardless of which thread executes the
sameThreadExecutor()
listener, all other registered but unexecuted
listeners are prevented from running during its execution, even if those
listeners are to run in other executors.
This is the most general listener interface. For common operations
performed using listeners, see Futures
. For a simplified but general
listener interface, see addCallback()
.
addListener
in interface ListenableFuture<V>
listener
- the listener to run when the computation is completeexec
- the executor to run the listener inprotected boolean set(@Nullable V value)
value
. This will set the state of the future to
AbstractFuture.Sync.COMPLETED
and invoke the listeners if the
state was successfully changed.value
- the value that was the result of the task.protected boolean setException(Throwable throwable)
throwable
. This will set the state of the future to
AbstractFuture.Sync.COMPLETED
and invoke the listeners if the
state was successfully changed.throwable
- the exception that the task failed with.Copyright © 2010-2014. All Rights Reserved.