Class SleepingIdleStrategy
java.lang.Object
org.agrona.concurrent.SleepingIdleStrategy
- All Implemented Interfaces:
IdleStrategy
When idle this strategy is to sleep for a specified period in nanoseconds.
If the period is zero or negative, no sleeping is done.
This class uses LockSupport.parkNanos(long) to idle.
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionDefault constructor usingDEFAULT_SLEEP_PERIOD_NS.SleepingIdleStrategy(long sleepPeriodNs) Constructed a new strategy that will sleep for a given period when idle.SleepingIdleStrategy(long sleepPeriod, TimeUnit timeUnit) Constructed a new strategy that will sleep for a given period when idle. -
Method Summary
Modifier and TypeMethodDescriptionalias()Simple name by which the strategy can be identified.voididle()Perform current idle action (e.g. nothing/yield/sleep).voididle(int workCount) Perform current idle action (e.g. nothing/yield/sleep).voidreset()Reset the internal state in preparation for entering an idle state again.toString()
-
Field Details
-
ALIAS
-
DEFAULT_SLEEP_PERIOD_NS
public static final long DEFAULT_SLEEP_PERIOD_NSDefault sleep period that tends to work as the likely minimum on Linux to be effective.- See Also:
-
-
Constructor Details
-
SleepingIdleStrategy
public SleepingIdleStrategy()Default constructor usingDEFAULT_SLEEP_PERIOD_NS. -
SleepingIdleStrategy
public SleepingIdleStrategy(long sleepPeriodNs) Constructed a new strategy that will sleep for a given period when idle.- Parameters:
sleepPeriodNs- period in nanosecond for which the strategy will sleep when work count is 0.
-
SleepingIdleStrategy
Constructed a new strategy that will sleep for a given period when idle.- Parameters:
sleepPeriod- the period for which the strategy will sleep when work count is 0.timeUnit- the timeunit of the sleepPeriod.
-
-
Method Details
-
idle
public void idle(int workCount) Perform current idle action (e.g. nothing/yield/sleep). This method signature expects users to call into it on every work 'cycle'. The implementations may use the indication "workCount > 0" to reset internal backoff state. This method works well with 'work' APIs which follow the following rules:- 'work' returns a value larger than 0 when some work has been done
- 'work' returns 0 when no work has been done
- 'work' may return error codes which are less than 0, but which amount to no work has been done
Callers are expected to follow this pattern:
while (isRunning) { idleStrategy.idle(doWork()); }- Specified by:
idlein interfaceIdleStrategy- Parameters:
workCount- performed in last duty cycle.
-
idle
public void idle()Perform current idle action (e.g. nothing/yield/sleep). To be used in conjunction withIdleStrategy.reset()to clear internal state when idle period is over (or before it begins). Callers are expected to follow this pattern:while (isRunning) { if (!hasWork()) { idleStrategy.reset(); while (!hasWork()) { if (!isRunning) { return; } idleStrategy.idle(); } } doWork(); }- Specified by:
idlein interfaceIdleStrategy
-
reset
public void reset()Reset the internal state in preparation for entering an idle state again.- Specified by:
resetin interfaceIdleStrategy
-
alias
Simple name by which the strategy can be identified.- Specified by:
aliasin interfaceIdleStrategy- Returns:
- simple name by which the strategy can be identified.
-
toString
-