Class SequentialExecutor

  • All Implemented Interfaces:
    java.util.concurrent.Executor

    @GwtIncompatible
    final class SequentialExecutor
    extends java.lang.Object
    implements java.util.concurrent.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 Detail

      • log

        private static final java.util.logging.Logger log
      • executor

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

        private final java.util.Deque<java.lang.Runnable> queue
      • 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.
    • Method Detail

      • execute

        public void execute​(java.lang.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 or to #resume() is made.

        Specified by:
        execute in interface java.util.concurrent.Executor
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object