Class AbstractFuture<V>
- java.lang.Object
-
- com.google.common.util.concurrent.AbstractFuture<V>
-
- All Implemented Interfaces:
ListenableFuture<V>,java.util.concurrent.Future<V>
- Direct Known Subclasses:
SettableFuture
public abstract class AbstractFuture<V> extends java.lang.Object implements ListenableFuture<V>
An abstract implementation of theListenableFutureinterface. This class is preferable toFutureTaskfor two reasons: It implementsListenableFuture, and it does not implementRunnable. (If you want aRunnableimplementation ofListenableFuture, create aListenableFutureTask, or submit your tasks to aListeningExecutorService.)This class implements all methods in
ListenableFuture. Subclasses should provide a way to set the result of the computation through the protected methodsset(Object)andsetException(Throwable). Subclasses may also overrideinterruptTask(), which will be invoked automatically if a call tocancel(true)succeeds in canceling the future.AbstractFutureuses anAbstractQueuedSynchronizerto 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
ExecutionListto 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-Executorpairs 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.)- Since:
- 1.0
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddListener(java.lang.Runnable listener, java.util.concurrent.Executor exec)Registers a listener to be run on the given executor.booleancancel(boolean mayInterruptIfRunning)Vget()Vget(long timeout, java.util.concurrent.TimeUnit unit)booleanisCancelled()booleanisDone()
-
-
-
Method Detail
-
get
public V get(long timeout, java.util.concurrent.TimeUnit unit) throws java.lang.InterruptedException, java.util.concurrent.TimeoutException, java.util.concurrent.ExecutionException
The default
AbstractFutureimplementation throwsInterruptedExceptionif the current thread is interrupted before or during the call, even if the value is already available.- Specified by:
getin interfacejava.util.concurrent.Future<V>- Throws:
java.lang.InterruptedException- if the current thread was interrupted before or during the call (optional but recommended).java.util.concurrent.CancellationExceptionjava.util.concurrent.TimeoutExceptionjava.util.concurrent.ExecutionException
-
get
public V get() throws java.lang.InterruptedException, java.util.concurrent.ExecutionException
The default
AbstractFutureimplementation throwsInterruptedExceptionif the current thread is interrupted before or during the call, even if the value is already available.- Specified by:
getin interfacejava.util.concurrent.Future<V>- Throws:
java.lang.InterruptedException- if the current thread was interrupted before or during the call (optional but recommended).java.util.concurrent.CancellationExceptionjava.util.concurrent.ExecutionException
-
isDone
public boolean isDone()
- Specified by:
isDonein interfacejava.util.concurrent.Future<V>
-
isCancelled
public boolean isCancelled()
- Specified by:
isCancelledin interfacejava.util.concurrent.Future<V>
-
cancel
public boolean cancel(boolean mayInterruptIfRunning)
- Specified by:
cancelin interfacejava.util.concurrent.Future<V>
-
addListener
public void addListener(java.lang.Runnable listener, java.util.concurrent.Executor exec)Registers a listener to be run on the given executor. The listener will run when theFuture'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., aRejectedExecutionExceptionor 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:- If this
Futureis done at the timeaddListeneris called,addListenerwill execute the listener inline. - If this
Futureis not yet done,addListenerwill schedule the listener to be run by the thread that completes thisFuture, 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, seeaddCallback().- Specified by:
addListenerin interfaceListenableFuture<V>- Parameters:
listener- the listener to run when the computation is completeexec- the executor to run the listener in- Since:
- 10.0
- If this
-
-