public abstract class BaseFuture<V> extends Object implements Future<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.)
| Constructor and Description |
|---|
BaseFuture() |
| Modifier and Type | Method and Description |
|---|---|
boolean |
cancel(boolean mayInterruptIfRunning) |
protected void |
done() |
V |
get() |
V |
get(long timeout,
TimeUnit unit) |
protected void |
interruptTask()
Subclasses can override this method to implement interruption of the
future's computation.
|
boolean |
isCancelled() |
boolean |
isDone() |
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. |
public V get(long timeout, TimeUnit unit) throws InterruptedException, TimeoutException, ExecutionException
The default BaseFuture 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).CancellationExceptionTimeoutExceptionExecutionExceptionpublic V get() throws InterruptedException, ExecutionException
The default BaseFuture 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).CancellationExceptionExecutionExceptionpublic boolean isCancelled()
isCancelled in interface Future<V>public boolean cancel(boolean mayInterruptIfRunning)
protected void interruptTask()
cancel(true).
The default implementation does nothing.
protected boolean set(@Nullable V value)
value. This will set the state of the future to
BaseFuture.Sync.COMPLETED and call done() 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
BaseFuture.Sync.COMPLETED and call done() if the
state was successfully changed.protected void done()
Copyright © 2009–2017. All rights reserved.