public class TimerWheel
extends java.lang.Object
Assumes single-writer principle and timers firing on processing thread. Low (or NO) garbage.
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 currently shrink.
Timer objects may be reused if desired, but all reuse must be done with timer cancellation, expiration, and timeouts in consideration.
Caveats
Timers that expire in the same tick will not be ordered with one another. As ticks are fairly large normally, this means that some timers may expire out of order.
| Modifier and Type | Class and Description |
|---|---|
class |
TimerWheel.Timer |
static class |
TimerWheel.TimerState |
| Modifier and Type | Field and Description |
|---|---|
static int |
INITIAL_TICK_DEPTH |
| Constructor and Description |
|---|
TimerWheel(long tickDuration,
java.util.concurrent.TimeUnit timeUnit,
int ticksPerWheel)
Construct a timer wheel for use in scheduling timers.
|
TimerWheel(NanoClock clock,
long tickDuration,
java.util.concurrent.TimeUnit timeUnit,
int ticksPerWheel)
Construct a timer wheel for use in scheduling timers.
|
| Modifier and Type | Method and Description |
|---|---|
NanoClock |
clock()
Get the
NanoClock used by this timer wheel. |
long |
computeDelayInMs()
Compute delay in milliseconds until next tick.
|
int |
expireTimers()
Process timers and execute any expired timers.
|
TimerWheel.Timer |
newBlankTimer()
Return a blank
TimerWheel.Timer suitable for rescheduling. |
TimerWheel.Timer |
newTimeout(long delay,
java.util.concurrent.TimeUnit unit,
java.lang.Runnable task)
Schedule a new timer that runs
task when it expires. |
void |
rescheduleTimeout(long delay,
java.util.concurrent.TimeUnit unit,
TimerWheel.Timer timer)
Reschedule an expired timer, reusing the
TimerWheel.Timer object. |
void |
rescheduleTimeout(long delay,
java.util.concurrent.TimeUnit unit,
TimerWheel.Timer timer,
java.lang.Runnable task)
Reschedule an expired timer, reusing the
TimerWheel.Timer object. |
public static final int INITIAL_TICK_DEPTH
public TimerWheel(long tickDuration,
java.util.concurrent.TimeUnit timeUnit,
int ticksPerWheel)
tickDuration - of each tick of the wheeltimeUnit - for the tick durationticksPerWheel - of the wheel. Must be a power of 2.public TimerWheel(NanoClock clock, long tickDuration, java.util.concurrent.TimeUnit timeUnit, int ticksPerWheel)
This constructor allows a custom function to return the current time instead of System.nanoTime().
clock - to use for system timetickDuration - of each tick of the wheeltimeUnit - for the tick durationticksPerWheel - of the wheel. Must be a power of 2.public NanoClock clock()
NanoClock used by this timer wheel.NanoClock used by this timer wheel.public TimerWheel.Timer newBlankTimer()
TimerWheel.Timer suitable for rescheduling.
NOTE: Appears to be a cancelled timer
public TimerWheel.Timer newTimeout(long delay, java.util.concurrent.TimeUnit unit, java.lang.Runnable task)
task when it expires.delay - until timer should expireunit - of time for delaytask - to execute when timer expiresTimerWheel.Timer for timerpublic void rescheduleTimeout(long delay,
java.util.concurrent.TimeUnit unit,
TimerWheel.Timer timer)
TimerWheel.Timer object.delay - until timer should expireunit - of time for delaytimer - to reschedulejava.lang.IllegalArgumentException - if timer is activepublic void rescheduleTimeout(long delay,
java.util.concurrent.TimeUnit unit,
TimerWheel.Timer timer,
java.lang.Runnable task)
TimerWheel.Timer object.delay - until timer should expireunit - of time for delaytimer - to rescheduletask - to execute when timer expiresjava.lang.IllegalArgumentException - if timer is activepublic long computeDelayInMs()
public int expireTimers()
Copyright © 2014 - 2016 Real Logic Ltd. All Rights Reserved.