Package com.lmax.disruptor
Class SleepingWaitStrategy
- java.lang.Object
-
- com.lmax.disruptor.SleepingWaitStrategy
-
- All Implemented Interfaces:
WaitStrategy
public final class SleepingWaitStrategy extends java.lang.Object implements WaitStrategy
Sleeping strategy that initially spins, then uses a Thread.yield(), and eventually sleep (LockSupport.parkNanos(n)
) for the minimum number of nanos the OS and JVM will allow while theEventProcessor
s are waiting on a barrier.This strategy is a good compromise between performance and CPU resource. Latency spikes can occur after quiet periods. It will also reduce the impact on the producing thread as it will not need signal any conditional variables to wake up the event handling thread.
-
-
Field Summary
Fields Modifier and Type Field Description private static int
DEFAULT_RETRIES
private static long
DEFAULT_SLEEP
private int
retries
private long
sleepTimeNs
-
Constructor Summary
Constructors Constructor Description SleepingWaitStrategy()
SleepingWaitStrategy(int retries)
SleepingWaitStrategy(int retries, long sleepTimeNs)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private int
applyWaitMethod(SequenceBarrier barrier, int counter)
void
signalAllWhenBlocking()
Implementations should signal the waitingEventProcessor
s that the cursor has advanced.long
waitFor(long sequence, Sequence cursor, Sequence dependentSequence, SequenceBarrier barrier)
Wait for the given sequence to be available.
-
-
-
Field Detail
-
DEFAULT_RETRIES
private static final int DEFAULT_RETRIES
- See Also:
- Constant Field Values
-
DEFAULT_SLEEP
private static final long DEFAULT_SLEEP
- See Also:
- Constant Field Values
-
retries
private final int retries
-
sleepTimeNs
private final long sleepTimeNs
-
-
Method Detail
-
waitFor
public long waitFor(long sequence, Sequence cursor, Sequence dependentSequence, SequenceBarrier barrier) throws AlertException
Description copied from interface:WaitStrategy
Wait for the given sequence to be available. It is possible for this method to return a value less than the sequence number supplied depending on the implementation of the WaitStrategy. A common use for this is to signal a timeout. Any EventProcessor that is using a WaitStrategy to get notifications about message becoming available should remember to handle this case. TheBatchEventProcessor
explicitly handles this case and will signal a timeout if required.- Specified by:
waitFor
in interfaceWaitStrategy
- Parameters:
sequence
- to be waited on.cursor
- the main sequence from ringbuffer. Wait/notify strategies will need this as it's the only sequence that is also notified upon update.dependentSequence
- on which to wait.barrier
- the processor is waiting on.- Returns:
- the sequence that is available which may be greater than the requested sequence.
- Throws:
AlertException
- if the status of the Disruptor has changed.
-
signalAllWhenBlocking
public void signalAllWhenBlocking()
Description copied from interface:WaitStrategy
Implementations should signal the waitingEventProcessor
s that the cursor has advanced.- Specified by:
signalAllWhenBlocking
in interfaceWaitStrategy
-
applyWaitMethod
private int applyWaitMethod(SequenceBarrier barrier, int counter) throws AlertException
- Throws:
AlertException
-
-