Class SerialWorkDispatcher
-
- All Implemented Interfaces:
public class SerialWorkDispatcher<T extends Object>Provides a template for processing a queue of work items serially. Allows adding new work items while the another item is being processed. Aims to separate the lifecycle of the worker thread that process work items with the queue that they are fetched from to allow sub-classes to be agnostic of worker thread management.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description public enumSerialWorkDispatcher.StateRepresents the state of the SerialWorkDispatcher.
public interfaceSerialWorkDispatcher.WorkHandlerRepresents the functional interface that is responsible for doing the desired work on each item of the workQueue. WorkHandler.doWork is called from the background worker thread that the SerialWorkDispatcher maintains.
-
Constructor Summary
Constructors Constructor Description SerialWorkDispatcher(String name, SerialWorkDispatcher.WorkHandler<T> workHandler)
-
Method Summary
Modifier and Type Method Description final UnitsetInitialJob(Runnable initialJob)Sets a job that is will be invoked (on the thread that SerialWorkDispatcher maintains) immediately when the SerialWorkDispatcher starts and before processing the items in the queue. final UnitsetFinalJob(Runnable finalJob)Sets a job that will be invoked ((on the thread that SerialWorkDispatcher maintains) immediately before the executorService is shutdown as a result of SerialWorkDispatcher.shutdown. final Booleanoffer(T item)Enqueues an item to the end of the workQueue. final Booleanstart()Puts the SerialWorkDispatcher in active state and starts processing the {@link #workQueue} if not already active. final Booleanpause()Puts the SerialWorkDispatcher in paused state and stops processing the {@link #workQueue} final Booleanresume()Resumes processing the work items in the workQueue if the SerialWorkDispatcher is active and if no worker thread is actively processing the workQueue. final Unitshutdown()Puts the SerialWorkDispatcher into inactive state and clears the workQueue. final SerialWorkDispatcher.StategetState()final UnitsetExecutorService(ExecutorService executorService)-
-
Constructor Detail
-
SerialWorkDispatcher
SerialWorkDispatcher(String name, SerialWorkDispatcher.WorkHandler<T> workHandler)
-
-
Method Detail
-
setInitialJob
final Unit setInitialJob(Runnable initialJob)
Sets a job that is will be invoked (on the thread that SerialWorkDispatcher maintains) immediately when the SerialWorkDispatcher starts and before processing the items in the queue. Implementers are expected to perform any one-time setup operations before processing starts. The initial job will only be executed if set before start() is invoked.
- Parameters:
initialJob- the Runnable that is to be invoked immediately before the SerialWorkDispatcher starts processing work items
-
setFinalJob
final Unit setFinalJob(Runnable finalJob)
Sets a job that will be invoked ((on the thread that SerialWorkDispatcher maintains) immediately before the executorService is shutdown as a result of SerialWorkDispatcher.shutdown. Implementers are expected to perform any cleanup operations when the SerialWorkDispatcher is shutdown. The final job will only be executed if set before shutdown() is invoked.
- Parameters:
finalJob- the Runnable that is to be invoked immediately before the SerialWorkDispatcher shuts down
-
offer
final Boolean offer(T item)
Enqueues an item to the end of the workQueue. Additionally, resumes the queue processing if the SerialWorkDispatcher is active (but processing was stopped earlier due to lack of work).
- Parameters:
item- item that needs to be processed.- Returns:
true if item has been enqueued successfully, false otherwise
-
start
final Boolean start()
Puts the SerialWorkDispatcher in active state and starts processing the {@link #workQueue} if not already active.
- Returns:
true - if SerialWorkDispatcher was successfully started, false - if it is already active or was shutdown
-
pause
final Boolean pause()
Puts the SerialWorkDispatcher in paused state and stops processing the {@link #workQueue}
- Returns:
true - if SerialWorkDispatcher was successfully stopped, false - if it is already stopped or was shutdown
-
resume
final Boolean resume()
Resumes processing the work items in the workQueue if the SerialWorkDispatcher is active and if no worker thread is actively processing the workQueue. Implementers can optionally trigger work via resume after any changes to logic in canWork now being true, without having to wait for the next item to be added.
-
shutdown
final Unit shutdown()
Puts the SerialWorkDispatcher into inactive state and clears the workQueue. Calling resume or start will have no effect on the state of the SerialWorkDispatcher after this method is invoked.
-
getState
final SerialWorkDispatcher.State getState()
-
setExecutorService
final Unit setExecutorService(ExecutorService executorService)
-
-
-
-