Class ExecutionSequencer
- java.lang.Object
-
- com.google.common.util.concurrent.ExecutionSequencer
-
public final class ExecutionSequencer extends Object
Serializes execution of tasks, somewhat like an "asynchronoussynchronizedblock." Each enqueued callable will not be submitted to its associated executor until the previous callable has returned -- and, if the previous callable was anAsyncCallable, not until theFutureit returned is done (successful, failed, or cancelled).This class serializes execution of submitted tasks but not any listeners of those tasks.
Submitted tasks have a happens-before order as defined in the Java Language Specification. Tasks execute with the same happens-before order that the function calls to
submit(java.util.concurrent.Callable<T>, java.util.concurrent.Executor)andsubmitAsync(com.google.common.util.concurrent.AsyncCallable<T>, java.util.concurrent.Executor)that submitted those tasks had.This class has limited support for cancellation and other "early completions":
- While calls to
submitandsubmitAsyncreturn aFuturethat can be cancelled, cancellation never propagates to a task that has started to run -- neither to the callable itself nor to anyFuturereturned by anAsyncCallable. (However, cancellation can prevent an unstarted task from running.) Therefore, the next task will wait for any running callable (or pendingFuturereturned by anAsyncCallable) to complete, without interrupting it (and without callingcancelon theFuture). So beware: Even if you cancel every precededingFuturereturned by this class, the next task may still have to wait.. - Once an
AsyncCallablereturns aFuture, this class considers that task to be "done" as soon as thatFuturecompletes in any way. Notably, aFutureis "completed" even if it is cancelled while its underlying work continues on a thread, an RPC, etc. TheFutureis also "completed" if it fails "early" -- for example, if the deadline expires on aFuturereturned fromFutures.withTimeout(com.google.common.util.concurrent.ListenableFuture<V>, java.time.Duration, java.util.concurrent.ScheduledExecutorService)while theFutureit wraps continues its underlying work. So beware: YourAsyncCallableshould not complete itsFutureuntil it is safe for the next task to start.
This class is similar to
MoreExecutors.newSequentialExecutor(java.util.concurrent.Executor). This class is different in a few ways:- Each task may be associated with a different executor.
- Tasks may be of type
AsyncCallable. - Running tasks cannot be interrupted. (Note that
newSequentialExecutordoes not returnFutureobjects, so it doesn't support interruption directly, either. However, utilities that use that executor have the ability to interrupt tasks running on it. This class, by contrast, does not expose anExecutorAPI.)
If you don't need the features of this class, you may prefer
newSequentialExecutorfor its simplicity and ability to accommodate interruption.- Since:
- 26.0
- While calls to
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static ExecutionSequencercreate()Creates a new instance.<T extends @Nullable Object>
ListenableFuture<T>submit(Callable<T> callable, Executor executor)Enqueues a task to run when the previous task (if any) completes.<T extends @Nullable Object>
ListenableFuture<T>submitAsync(AsyncCallable<T> callable, Executor executor)Enqueues a task to run when the previous task (if any) completes.
-
-
-
Method Detail
-
create
public static ExecutionSequencer create()
Creates a new instance.
-
submit
public <T extends @Nullable Object> ListenableFuture<T> submit(Callable<T> callable, Executor executor)
Enqueues a task to run when the previous task (if any) completes.Cancellation does not propagate from the output future to a callable that has begun to execute, but if the output future is cancelled before
Callable.call()is invoked,Callable.call()will not be invoked.
-
submitAsync
public <T extends @Nullable Object> ListenableFuture<T> submitAsync(AsyncCallable<T> callable, Executor executor)
Enqueues a task to run when the previous task (if any) completes.Cancellation does not propagate from the output future to the future returned from
callableor a callable that has begun to execute, but if the output future is cancelled beforeAsyncCallable.call()is invoked,AsyncCallable.call()will not be invoked.
-
-