public class DeadlineTimerWheel
extends java.lang.Object
Based on netty's HashedTimerWheel, which is based on George Varghese and Tony Lauck's paper, 'Hashed and Hierarchical Timing Wheels: data structures to efficiently implement a timer facility'. More comprehensive slides are located here.
Wheel is backed by arrays. Timer cancellation is O(1). Timer scheduling might be slightly longer if a lot of timers are in the same tick. The underlying tick contains an array. That array grows when needed, but does not shrink.
Caveats
Timers that expire in the same tick will not be ordered with one another. As ticks are fairly coarse resolution normally, this means that some timers may expire out of order.
Note: Not threadsafe.
Modifier and Type | Class and Description |
---|---|
static interface |
DeadlineTimerWheel.TimerConsumer
Consumer of timer entries as deadline to timerId.
|
static interface |
DeadlineTimerWheel.TimerHandler
Handler for expired timers.
|
Modifier and Type | Field and Description |
---|---|
static long |
NULL_TIMER |
Constructor and Description |
---|
DeadlineTimerWheel(java.util.concurrent.TimeUnit timeUnit,
long startTime,
int tickResolution,
int ticksPerWheel)
Construct timer wheel with given parameters.
|
DeadlineTimerWheel(java.util.concurrent.TimeUnit timeUnit,
long startTime,
int tickResolution,
int ticksPerWheel,
int initialTickAllocation)
Construct timer wheel with given parameters.
|
Modifier and Type | Method and Description |
---|---|
boolean |
cancelTimer(long timerId)
Cancel a previously scheduled timer.
|
long |
currentTickTime()
Time of current tick of the wheel in
timeUnit() s. |
long |
deadline(long timerId)
Return the deadline for the given timerId.
|
void |
forEach(DeadlineTimerWheel.TimerConsumer consumer)
Iterate over wheel so all active timers can be consumed without expiring them.
|
int |
poll(long now,
DeadlineTimerWheel.TimerHandler handler,
int maxTimersToExpire)
Poll for timers expired by the deadline passing.
|
void |
resetStartTime(long startTime)
Reset the start time of the wheel.
|
long |
scheduleTimer(long deadline)
Schedule a timer for a given absolute time as a deadline in
timeUnit() s. |
long |
tickResolution()
Resolution of a tick of the wheel in
timeUnit() s. |
long |
timerCount()
Number of active timers.
|
java.util.concurrent.TimeUnit |
timeUnit()
Time unit for the time values.
|
public static final long NULL_TIMER
public DeadlineTimerWheel(java.util.concurrent.TimeUnit timeUnit, long startTime, int tickResolution, int ticksPerWheel)
timeUnit
- for the values used to express the time.startTime
- for the wheel (in given TimeUnit
)tickResolution
- for the wheel, i.e. how many TimeUnit
s per tick.ticksPerWheel
- for the wheel (must be power of 2)public DeadlineTimerWheel(java.util.concurrent.TimeUnit timeUnit, long startTime, int tickResolution, int ticksPerWheel, int initialTickAllocation)
timeUnit
- for the values used to express the time.startTime
- for the wheel (in given TimeUnit
)tickResolution
- for the wheel, i.e. how many TimeUnit
s per tick.ticksPerWheel
- for the wheel (must be power of 2)initialTickAllocation
- space allocated in the wheel.public java.util.concurrent.TimeUnit timeUnit()
public long tickResolution()
timeUnit()
s.timeUnit()
s.public long currentTickTime()
timeUnit()
s.timeUnit()
s.public long timerCount()
public void resetStartTime(long startTime)
startTime
- to set the wheel to.java.lang.IllegalStateException
- if wheel has any active timers.public long scheduleTimer(long deadline)
timeUnit()
s. A timerId will be assigned
and returned for future reference.deadline
- after which the timer should expire.public boolean cancelTimer(long timerId)
timerId
- of the timer to cancel.public int poll(long now, DeadlineTimerWheel.TimerHandler handler, int maxTimersToExpire)
now
- current time to compare deadlines against.handler
- to call for each expired timer.maxTimersToExpire
- to process in one poll operation.public void forEach(DeadlineTimerWheel.TimerConsumer consumer)
consumer
- to call for each active timer.public long deadline(long timerId)
timerId
- of the timer to return the deadline of.NULL_TIMER
if timerId is not running.Copyright © 2014-2019 Real Logic Ltd. All Rights Reserved.