public final class BackoffIdleStrategy extends Object implements IdleStrategy
Spin for maxSpins, then
Thread.yield() for maxYields, then
LockSupport.parkNanos(long) on an exponential backoff to maxParkPeriodNs
| Modifier and Type | Field and Description |
|---|---|
static String |
ALIAS
Name to be returned from
alias(). |
static long |
DEFAULT_MAX_PARK_PERIOD_NS
Default maximum interval the strategy will park a thread.
|
static long |
DEFAULT_MAX_SPINS
Default number of times the strategy will spin without work before going to next state.
|
static long |
DEFAULT_MAX_YIELDS
Default number of times the strategy will yield without work before going to next state.
|
static long |
DEFAULT_MIN_PARK_PERIOD_NS
Default minimum interval the strategy will park a thread.
|
protected long |
maxParkPeriodNs
Max park period in nanoseconds.
|
protected long |
maxSpins
Max number of spins.
|
protected long |
maxYields
Max number of yields.
|
protected long |
minParkPeriodNs
Min park period in nanoseconds.
|
protected static int |
NOT_IDLE
Denotes a non-idle state.
|
protected static int |
PARKING
Denotes a parking state.
|
protected long |
parkPeriodNs
Park period in nanoseconds.
|
protected static int |
SPINNING
Denotes a spinning state.
|
protected long |
spins
Number of spins.
|
protected int |
state
Current state.
|
protected static int |
YIELDING
Denotes a yielding state.
|
protected long |
yields
Number of yields.
|
| Constructor and Description |
|---|
BackoffIdleStrategy()
Default constructor using
DEFAULT_MAX_SPINS, DEFAULT_MAX_YIELDS,
DEFAULT_MIN_PARK_PERIOD_NS, and DEFAULT_MAX_YIELDS. |
BackoffIdleStrategy(long maxSpins,
long maxYields,
long minParkPeriodNs,
long maxParkPeriodNs)
Create a set of state tracking idle behavior
|
| Modifier and Type | Method and Description |
|---|---|
String |
alias()
Simple name by which the strategy can be identified.
|
void |
idle()
Perform current idle action (e.g.
|
void |
idle(int workCount)
Perform current idle action (e.g.
|
void |
reset()
Reset the internal state in preparation for entering an idle state again.
|
String |
toString() |
public static final String ALIAS
alias().public static final long DEFAULT_MAX_SPINS
public static final long DEFAULT_MAX_YIELDS
public static final long DEFAULT_MIN_PARK_PERIOD_NS
public static final long DEFAULT_MAX_PARK_PERIOD_NS
protected static final int NOT_IDLE
protected static final int SPINNING
protected static final int YIELDING
protected static final int PARKING
protected final long maxSpins
protected final long maxYields
protected final long minParkPeriodNs
protected final long maxParkPeriodNs
protected int state
protected long spins
protected long yields
protected long parkPeriodNs
public BackoffIdleStrategy()
DEFAULT_MAX_SPINS, DEFAULT_MAX_YIELDS,
DEFAULT_MIN_PARK_PERIOD_NS, and DEFAULT_MAX_YIELDS.public BackoffIdleStrategy(long maxSpins,
long maxYields,
long minParkPeriodNs,
long maxParkPeriodNs)
maxSpins - to perform before moving to Thread.yield()maxYields - to perform before moving to LockSupport.parkNanos(long)minParkPeriodNs - to use when initiating parkingmaxParkPeriodNs - to use when parkingpublic void idle(int workCount)
Callers are expected to follow this pattern:
while (isRunning)
{
idleStrategy.idle(doWork());
}
idle in interface IdleStrategyworkCount - performed in last duty cycle.public void idle()
IdleStrategy.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();
}
idle in interface IdleStrategypublic void reset()
reset in interface IdleStrategypublic String alias()
alias in interface IdleStrategyCopyright © 2014-2021 Real Logic Limited. All Rights Reserved.