Class BackoffIdleStrategy
- All Implemented Interfaces:
IdleStrategy
Spin for maxSpins, then
Thread.yield() for maxYields, then
LockSupport.parkNanos(long) on an exponential backoff to maxParkPeriodNs.
Under Linux, multiple timer events will be coalesced in a 50 us window to minimize the timer overhead
on the CPU. E.g. if you want to wait 10 us, it could be you need to wait 50us. This situation can be
improved by changing the value of the timerslack_ns property which defaults to 50000. This can be done
like this:
echo 10000 > /proc/PID/timerslack_ns
This will set the timer slack to 10 microseconds for the given PID of the thread. This property
can't be set at the process level, so needs to be set for each thread specifically. Also, it isn't
guaranteed that after setting the property, the waiting time will be respected.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringName to be returned fromalias().static final longDefault maximum interval the strategy will park a thread.static final longDefault number of times the strategy will spin without work before going to next state.static final longDefault number of times the strategy will yield without work before going to next state.static final longDefault minimum interval the strategy will park a thread.protected final longMax park period in nanoseconds.protected final longMax number of spins.protected final longMax number of yields.protected final longMin park period in nanoseconds.protected static final intDenotes a non-idle state.protected static final intDenotes a parking state.protected longPark period in nanoseconds.protected static final intDenotes a spinning state.protected longNumber of spins.protected intCurrent state.protected static final intDenotes a yielding state.protected longNumber of yields. -
Constructor Summary
ConstructorsConstructorDescriptionDefault constructor usingDEFAULT_MAX_SPINS,DEFAULT_MAX_YIELDS,DEFAULT_MIN_PARK_PERIOD_NS, andDEFAULT_MAX_YIELDS.BackoffIdleStrategy(long maxSpins, long maxYields, long minParkPeriodNs, long maxParkPeriodNs) Create a set of state tracking idle behavior. -
Method Summary
Modifier and TypeMethodDescriptionalias()Simple name by which the strategy can be identified.voididle()Perform current idle action (e.g.voididle(int workCount) Perform current idle action (e.g.voidreset()Reset the internal state in preparation for entering an idle state again.toString()
-
Field Details
-
ALIAS
Name to be returned fromalias().- See Also:
-
DEFAULT_MAX_SPINS
public static final long DEFAULT_MAX_SPINSDefault number of times the strategy will spin without work before going to next state.- See Also:
-
DEFAULT_MAX_YIELDS
public static final long DEFAULT_MAX_YIELDSDefault number of times the strategy will yield without work before going to next state.- See Also:
-
DEFAULT_MIN_PARK_PERIOD_NS
public static final long DEFAULT_MIN_PARK_PERIOD_NSDefault minimum interval the strategy will park a thread.- See Also:
-
DEFAULT_MAX_PARK_PERIOD_NS
public static final long DEFAULT_MAX_PARK_PERIOD_NSDefault maximum interval the strategy will park a thread.- See Also:
-
NOT_IDLE
protected static final int NOT_IDLEDenotes a non-idle state.- See Also:
-
SPINNING
protected static final int SPINNINGDenotes a spinning state.- See Also:
-
YIELDING
protected static final int YIELDINGDenotes a yielding state.- See Also:
-
PARKING
protected static final int PARKINGDenotes a parking state.- See Also:
-
maxSpins
protected final long maxSpinsMax number of spins. -
maxYields
protected final long maxYieldsMax number of yields. -
minParkPeriodNs
protected final long minParkPeriodNsMin park period in nanoseconds. -
maxParkPeriodNs
protected final long maxParkPeriodNsMax park period in nanoseconds. -
state
protected int stateCurrent state. -
spins
protected long spinsNumber of spins. -
yields
protected long yieldsNumber of yields. -
parkPeriodNs
protected long parkPeriodNsPark period in nanoseconds.
-
-
Constructor Details
-
BackoffIdleStrategy
public BackoffIdleStrategy()Default constructor usingDEFAULT_MAX_SPINS,DEFAULT_MAX_YIELDS,DEFAULT_MIN_PARK_PERIOD_NS, andDEFAULT_MAX_YIELDS. -
BackoffIdleStrategy
public BackoffIdleStrategy(long maxSpins, long maxYields, long minParkPeriodNs, long maxParkPeriodNs) Create a set of state tracking idle behavior.- Parameters:
maxSpins- to perform before moving toThread.yield()maxYields- to perform before moving toLockSupport.parkNanos(long)minParkPeriodNs- to use when initiating parkingmaxParkPeriodNs- to use when parking
-
-
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
-