Class SequentialExecutor

java.lang.Object
com.google.common.util.concurrent.SequentialExecutor
All Implemented Interfaces:
Executor

final class SequentialExecutor extends Object implements Executor
Executor ensuring that all Runnables submitted are executed in order, using the provided Executor, and sequentially such that no two will ever be running at the same time.

Tasks submitted to execute(Runnable) are executed in FIFO order.

The execution of tasks is done by one thread as long as there are tasks left in the queue. When a task is interrupted, execution of subsequent tasks continues. See SequentialExecutor.QueueWorker.workOnQueue() for details.

RuntimeExceptions thrown by tasks are simply logged and the executor keeps trucking. If an Error is thrown, the error will propagate and execution will stop until it is restarted by a call to execute(java.lang.Runnable).

  • Field Details

    • log

      private static final LazyLogger log
    • executor

      private final Executor executor
      Underlying executor that all submitted Runnable objects are run on.
    • queue

      private final Deque<Runnable> queue
    • workerRunningState

      private SequentialExecutor.WorkerRunningState workerRunningState
    • workerRunCount

      private long workerRunCount
      This counter prevents an ABA issue where a thread may successfully schedule the worker, the worker runs and exhausts the queue, another thread enqueues a task and fails to schedule the worker, and then the first thread's call to delegate.execute() returns. Without this counter, it would observe the QUEUING state and set it to QUEUED, and the worker would never be scheduled again for future submissions.
    • worker

      private final SequentialExecutor.QueueWorker worker
  • Constructor Details

  • Method Details

    • execute

      public void execute(Runnable task)
      Adds a task to the queue and makes sure a worker thread is running.

      If this method throws, e.g. a RejectedExecutionException from the delegate executor, execution of tasks will stop until a call to this method is made.

      Specified by:
      execute in interface Executor
    • toString

      public String toString()
      Overrides:
      toString in class Object