Class SequentialExecutor
- java.lang.Object
-
- com.google.common.util.concurrent.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.RuntimeException
s thrown by tasks are simply logged and the executor keeps trucking. If anError
is thrown, the error will propagate and execution will stop until it is restarted by a call toexecute(java.lang.Runnable)
.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private class
SequentialExecutor.QueueWorker
Worker that runs tasks fromqueue
until it is empty.(package private) static class
SequentialExecutor.WorkerRunningState
-
Field Summary
Fields Modifier and Type Field Description private java.util.concurrent.Executor
executor
Underlying executor that all submitted Runnable objects are run on.private static java.util.logging.Logger
log
private java.util.Deque<java.lang.Runnable>
queue
private SequentialExecutor.QueueWorker
worker
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.private SequentialExecutor.WorkerRunningState
workerRunningState
-
Constructor Summary
Constructors Constructor Description SequentialExecutor(java.util.concurrent.Executor executor)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
execute(java.lang.Runnable task)
Adds a task to the queue and makes sure a worker thread is running.java.lang.String
toString()
-
-
-
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
-
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
-
-
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 interfacejava.util.concurrent.Executor
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
-