Class SerializingExecutor
- java.lang.Object
-
- com.google.common.util.concurrent.SerializingExecutor
-
- All Implemented Interfaces:
java.util.concurrent.Executor
@GwtIncompatible final class SerializingExecutor extends java.lang.Object implements java.util.concurrent.Executor
Executor ensuring that all Runnables submitted are executed in order, using the provided Executor, and serially such that no two will ever be running at the same time.Tasks submitted to
execute(Runnable)are executed in FIFO order.Tasks can also be prepended to the queue to be executed in LIFO order before any other submitted tasks. Primarily intended for the currently executing task to be able to schedule a continuation task.
Execution on the queue can be suspended, e.g. while waiting for an RPC, and execution can be resumed later.
The execution of tasks is done by one thread as long as there are tasks left in the queue and execution has not been suspended. (Even if one task is interrupted, execution of subsequent tasks continues.)
RuntimeExceptions thrown by tasks are simply logged and the executor keeps trucking. If anErroris thrown, the error will propagate and execution will stop until it is restarted by external calls.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private classSerializingExecutor.QueueWorkerWorker that runs tasks off the queue until it is empty or the queue is suspended.
-
Field Summary
Fields Modifier and Type Field Description private java.util.concurrent.ExecutorexecutorUnderlying executor that all submitted Runnable objects are run on.private java.lang.ObjectinternalLockprivate booleanisWorkerRunningprivate static java.util.logging.Loggerlogprivate java.util.Deque<java.lang.Runnable>queueprivate intsuspensions
-
Constructor Summary
Constructors Constructor Description SerializingExecutor(java.util.concurrent.Executor executor)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidexecute(java.lang.Runnable task)Adds a task to the queue and makes sure a worker thread is running, unless the queue has been suspended.voidexecuteFirst(java.lang.Runnable task)Prepends a task to the front of the queue and makes sure a worker thread is running, unless the queue has been suspended.voidresume()Continue execution of tasks after a call tosuspend().private voidstartQueueWorker()voidsuspend()Suspends the running of tasks untilresume()is called.
-
-
-
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
-
isWorkerRunning
private boolean isWorkerRunning
-
suspensions
private int suspensions
-
internalLock
private final java.lang.Object internalLock
-
-
Method Detail
-
execute
public void execute(java.lang.Runnable task)
Adds a task to the queue and makes sure a worker thread is running, unless the queue has been suspended.If this method throws, e.g. a
RejectedExecutionExceptionfrom the delegate executor, execution of tasks will stop until a call to this method or toresume()is made.- Specified by:
executein interfacejava.util.concurrent.Executor
-
executeFirst
public void executeFirst(java.lang.Runnable task)
Prepends a task to the front of the queue and makes sure a worker thread is running, unless the queue has been suspended.
-
suspend
public void suspend()
Suspends the running of tasks untilresume()is called. This can be called multiple times to increase the suspensions count and execution will not continue untilresume()has been called the same number of times assuspendhas been.Any task that has already been pulled off the queue for execution will be completed before execution is suspended.
-
resume
public void resume()
Continue execution of tasks after a call tosuspend(). More accurately, decreases the suspension counter, as has been incremented by calls tosuspend(), and resumes execution if the suspension counter is zero.If this method throws, e.g. a
RejectedExecutionExceptionfrom the delegate executor, execution of tasks will stop until a call to this method or toexecute(Runnable)orexecuteFirst(Runnable)is made.- Throws:
java.lang.IllegalStateException- if this executor is not suspended.
-
startQueueWorker
private void startQueueWorker()
-
-