public class ExponentialTimer extends Object
ExponentialTimer
can be used for generating a sequence of events that are
exponentially distributed in time.
The intended usage is to associate the timer with an operation that should to be re-attempted
if it fails using exponential back-off. For instance, an event loop iterates over scheduled
operations each of which is associated with a timer and the event loop can use the timer to check
if the operation is ready to be attempted and whether it should be re-attempted again in the
future in case it fails.
while (true) { Operation op = operations.pop(); switch (op.getTimer().tick()) { case EXPIRED: // operation will not be re-attempted break; case NOT_READY: // operation is not ready to be re-attempted operations.push(op); break; case READY: boolean success = op.run(); if (!success) { operations.push(op); } } Thread.sleep(10); }Note that in the above scenario, the
ExponentialBackoffRetry
policy cannot be used
because the RetryPolicy#attemptRetry()
is blocking.Modifier and Type | Class and Description |
---|---|
static class |
ExponentialTimer.Result
Represents the result of
tick() . |
Constructor and Description |
---|
ExponentialTimer(long initialIntervalMs,
long maxIntervalMs,
long initialWaitTimeMs,
long maxTotalWaitTimeMs)
Creates a new instance of
ExponentialTimer . |
Modifier and Type | Method and Description |
---|---|
long |
getNumEvents() |
ExponentialTimer.Result |
tick()
Attempts to perform a timer tick.
|
public ExponentialTimer(long initialIntervalMs, long maxIntervalMs, long initialWaitTimeMs, long maxTotalWaitTimeMs)
ExponentialTimer
.initialIntervalMs
- the initial interval between events (in milliseconds)maxIntervalMs
- the maximum interval between events (in milliseconds)initialWaitTimeMs
- the initial wait time before first event (in milliseconds)maxTotalWaitTimeMs
- the maximum total wait time (in milliseconds)public long getNumEvents()
public ExponentialTimer.Result tick()
ExponentialTimer.Result
that indicates which scenario was encounteredCopyright © 2023. All Rights Reserved.