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.
|
Constructor and Description |
---|
DeadlineTimerWheel(java.util.concurrent.TimeUnit timeUnit,
long startTime,
int resolution,
int ticksPerWheel)
Construct timer wheel with given parameters.
|
DeadlineTimerWheel(java.util.concurrent.TimeUnit timeUnit,
long startTime,
int resolution,
int ticksPerWheel,
int initialTickAllocation)
Construct timer wheel with given parameters.
|
Modifier and Type | Method and Description |
---|---|
void |
cancelTimer(long timerId)
Cancel a previously scheduled timer.
|
long |
currentTickTime()
Time of current tick of the wheel in
timeUnit() s. |
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.
|
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 DeadlineTimerWheel(java.util.concurrent.TimeUnit timeUnit, long startTime, int resolution, int ticksPerWheel)
timeUnit
- for the values used to express the time.startTime
- for the wheel (in given TimeUnit
)resolution
- 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 resolution, int ticksPerWheel, int initialTickAllocation)
timeUnit
- for the values used to express the time.startTime
- for the wheel (in given TimeUnit
)resolution
- 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 long scheduleTimer(long deadline)
timeUnit()
s. A timerId will be assigned
and returned for future reference.deadline
- after which the timer should expire.public void 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.Copyright © 2014-2017 Real Logic Ltd. All Rights Reserved.