Package com.lmax.disruptor
Class WorkerPool<T>
- java.lang.Object
-
- com.lmax.disruptor.WorkerPool<T>
-
- Type Parameters:
T
- event to be processed by a pool of workers
public final class WorkerPool<T> extends java.lang.Object
WorkerPool contains a pool ofWorkProcessor
s that will consume sequences so jobs can be farmed out across a pool of workers. Each of theWorkProcessor
s manage and calls aWorkHandler
to process the events.
-
-
Field Summary
Fields Modifier and Type Field Description private RingBuffer<T>
ringBuffer
private java.util.concurrent.atomic.AtomicBoolean
started
private WorkProcessor<?>[]
workProcessors
private Sequence
workSequence
-
Constructor Summary
Constructors Constructor Description WorkerPool(EventFactory<T> eventFactory, ExceptionHandler<? super T> exceptionHandler, WorkHandler<? super T>... workHandlers)
Construct a work pool with an internalRingBuffer
for convenience.WorkerPool(RingBuffer<T> ringBuffer, SequenceBarrier sequenceBarrier, ExceptionHandler<? super T> exceptionHandler, WorkHandler<? super T>... workHandlers)
Create a worker pool to enable an array ofWorkHandler
s to consume published sequences.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
drainAndHalt()
Wait for theRingBuffer
to drain of published events then halt the workers.Sequence[]
getWorkerSequences()
Get an array ofSequence
s representing the progress of the workers.void
halt()
Halt all workers immediately at the end of their current cycle.boolean
isRunning()
RingBuffer<T>
start(java.util.concurrent.Executor executor)
Start the worker pool processing events in sequence.
-
-
-
Field Detail
-
started
private final java.util.concurrent.atomic.AtomicBoolean started
-
workSequence
private final Sequence workSequence
-
ringBuffer
private final RingBuffer<T> ringBuffer
-
workProcessors
private final WorkProcessor<?>[] workProcessors
-
-
Constructor Detail
-
WorkerPool
@SafeVarargs public WorkerPool(RingBuffer<T> ringBuffer, SequenceBarrier sequenceBarrier, ExceptionHandler<? super T> exceptionHandler, WorkHandler<? super T>... workHandlers)
Create a worker pool to enable an array ofWorkHandler
s to consume published sequences.This option requires a pre-configured
RingBuffer
which must haveRingBuffer.addGatingSequences(Sequence...)
called before the work pool is started.- Parameters:
ringBuffer
- of events to be consumed.sequenceBarrier
- on which the workers will depend.exceptionHandler
- to callback when an error occurs which is not handled by theWorkHandler
s.workHandlers
- to distribute the work load across.
-
WorkerPool
@SafeVarargs public WorkerPool(EventFactory<T> eventFactory, ExceptionHandler<? super T> exceptionHandler, WorkHandler<? super T>... workHandlers)
Construct a work pool with an internalRingBuffer
for convenience.This option does not require
RingBuffer.addGatingSequences(Sequence...)
to be called before the work pool is started.- Parameters:
eventFactory
- for filling theRingBuffer
exceptionHandler
- to callback when an error occurs which is not handled by theWorkHandler
s.workHandlers
- to distribute the work load across.
-
-
Method Detail
-
getWorkerSequences
public Sequence[] getWorkerSequences()
Get an array ofSequence
s representing the progress of the workers.- Returns:
- an array of
Sequence
s representing the progress of the workers.
-
start
public RingBuffer<T> start(java.util.concurrent.Executor executor)
Start the worker pool processing events in sequence.- Parameters:
executor
- providing threads for running the workers.- Returns:
- the
RingBuffer
used for the work queue. - Throws:
java.lang.IllegalStateException
- if the pool has already been started and not halted yet
-
drainAndHalt
public void drainAndHalt()
Wait for theRingBuffer
to drain of published events then halt the workers.
-
halt
public void halt()
Halt all workers immediately at the end of their current cycle.
-
isRunning
public boolean isRunning()
-
-