Class ExecutionSequencer
synchronized block." Each
enqueued callable will not be submitted to its associated executor until the
previous callable has returned -- and, if the previous callable was an AsyncCallable, not
until the Future it 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(Callable, Executor) and
submitAsync(AsyncCallable, 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 precedingFuturereturned 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(ListenableFuture, Duration, 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(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 newSequentialExecutor for
its simplicity and ability to accommodate interruption.
- Since:
- 26.0
-
Method Summary
Modifier and TypeMethodDescriptionstatic ExecutionSequencercreate()Creates a new instance.<T extends @Nullable Object>
ListenableFuture<T> 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 Details
-
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.
-