Class ExecutionSequencer
- java.lang.Object
-
- com.google.common.util.concurrent.ExecutionSequencer
-
public final class ExecutionSequencer extends java.lang.ObjectSerializes 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 has limited support for cancellation and other "early completion":
- 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.
An additional limitation: this class serializes execution of tasks but not any listeners of those tasks.
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
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static classExecutionSequencer.RunningStateprivate static classExecutionSequencer.TaskNonReentrantExecutorThis class helps avoid a StackOverflowError when large numbers of tasks are submitted withMoreExecutors.directExecutor().private static classExecutionSequencer.ThreadConfinedTaskQueueThis object is unsafely published, but avoids problematic races by relying exclusively on the identity equality of its Thread field so that the task field is only accessed by a single thread.
-
Field Summary
Fields Modifier and Type Field Description private ExecutionSequencer.ThreadConfinedTaskQueuelatestTaskQueueprivate java.util.concurrent.atomic.AtomicReference<ListenableFuture<java.lang.Void>>refThis reference acts as a pointer tracking the head of a linked list of ListenableFutures.
-
Constructor Summary
Constructors Modifier Constructor Description privateExecutionSequencer()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static ExecutionSequencercreate()Creates a new instance.<T> ListenableFuture<T>submit(java.util.concurrent.Callable<T> callable, java.util.concurrent.Executor executor)Enqueues a task to run when the previous task (if any) completes.<T> ListenableFuture<T>submitAsync(AsyncCallable<T> callable, java.util.concurrent.Executor executor)Enqueues a task to run when the previous task (if any) completes.
-
-
-
Field Detail
-
ref
private final java.util.concurrent.atomic.AtomicReference<ListenableFuture<java.lang.Void>> ref
This reference acts as a pointer tracking the head of a linked list of ListenableFutures.
-
latestTaskQueue
private ExecutionSequencer.ThreadConfinedTaskQueue latestTaskQueue
-
-
Method Detail
-
create
public static ExecutionSequencer create()
Creates a new instance.
-
submit
public <T> ListenableFuture<T> submit(java.util.concurrent.Callable<T> callable, java.util.concurrent.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> ListenableFuture<T> submitAsync(AsyncCallable<T> callable, java.util.concurrent.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.
-
-