|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object com.google.common.util.concurrent.AbstractFuture<V>
@Beta public abstract class AbstractFuture<V>
An abstract implementation of the Future
interface. This class
is an abstraction of FutureTask
to support use
for tasks other than Runnable
s. It uses an
AbstractQueuedSynchronizer
to deal with concurrency issues and
guarantee thread safety. It could be used as a base class to
FutureTask
, or any other implementor of the Future
interface.
This class implements all methods in Future
. Subclasses should
provide a way to set the result of the computation through the protected
methods set(Object)
, setException(Throwable)
, or
cancel()
. If subclasses want to implement cancellation they can
override the cancel(boolean)
method with a real implementation, the
default implementation doesn't support cancellation.
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. Because this class does not implement cancellation it is left to the subclass to distinguish between created and running tasks.
Constructor Summary | |
---|---|
AbstractFuture()
|
Method Summary | |
---|---|
protected boolean |
cancel()
Subclasses should invoke this method to mark the future as cancelled. |
boolean |
cancel(boolean mayInterruptIfRunning)
Attempts to cancel execution of this task. |
protected void |
done()
|
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. |
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 . |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public AbstractFuture()
Method Detail |
---|
public V get(long timeout, TimeUnit unit) throws InterruptedException, TimeoutException, ExecutionException
java.util.concurrent.Future
get
in interface Future<V>
timeout
- the maximum time to waitunit
- the time unit of the timeout argument
InterruptedException
- if the current thread was interrupted
while waiting
TimeoutException
- if the wait timed out
ExecutionException
- if the computation threw an
exceptionpublic V get() throws InterruptedException, ExecutionException
java.util.concurrent.Future
get
in interface Future<V>
InterruptedException
- if the current thread was interrupted
while waiting
ExecutionException
- if the computation threw an
exceptionpublic boolean isDone()
java.util.concurrent.Future
isDone
in interface Future<V>
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 complete
protected boolean set(@Nullable V value)
value
. This will set the state of the future to
AbstractFuture.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
AbstractFuture.Sync.COMPLETED
and call done()
if the
state was successfully changed.
throwable
- the exception that the task failed with.
Error
- if the throwable was an Error
.protected final boolean cancel()
AbstractFuture.Sync.CANCELLED
and call done()
if the state was
successfully changed.
protected void done()
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |