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 long |
DEFAULT_MAX_PARK_PERIOD_NS |
static long |
DEFAULT_MAX_SPINS |
static long |
DEFAULT_MAX_YIELDS |
static long |
DEFAULT_MIN_PARK_PERIOD_NS |
protected long |
maxParkPeriodNs |
protected long |
maxSpins |
protected long |
maxYields |
protected long |
minParkPeriodNs |
protected static int |
NOT_IDLE |
protected static int |
PARKING |
protected long |
parkPeriodNs |
protected static int |
SPINNING |
protected long |
spins |
protected int |
state |
protected static int |
YIELDING |
protected long |
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 |
---|---|
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 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 IdleStrategy
workCount
- 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 IdleStrategy
public void reset()
reset
in interface IdleStrategy
Copyright © 2014-2019 Real Logic Ltd. All Rights Reserved.