public class HashedWheelTimer extends java.lang.Object implements Timer
Timer optimized for approximated I/O timeout scheduling.
TimerTask on time. HashedWheelTimer, on every tick, will
check if there are any TimerTasks behind the schedule and execute
them.
You can increase or decrease the accuracy of the execution timing by specifying smaller or larger tick duration in the constructor. In most network applications, I/O timeout does not need to be accurate. Therefore, the default tick duration is 100 milliseconds and you will not need to try different configurations in most cases.
HashedWheelTimer maintains a data structure called 'wheel'.
To put simply, a wheel is a hash table of TimerTasks whose hash
function is 'dead line of the task'. The default number of ticks per wheel
(i.e. the size of the wheel) is 512. You could specify a larger value
if you are going to schedule a lot of timeouts.
HashedWheelTimer creates a new thread whenever it is instantiated and
started. Therefore, you should make sure to create only one instance and
share it across your application. One of the common mistakes, that makes
your application unresponsive, is to create a new instance in
ChannelPipelineFactory, which results in the creation of a new thread
for every connection.
HashedWheelTimer 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.| Modifier and Type | Class and Description |
|---|---|
private static class |
HashedWheelTimer.HashedWheelBucket
Bucket that stores HashedWheelTimeouts.
|
private static class |
HashedWheelTimer.HashedWheelTimeout |
private class |
HashedWheelTimer.Worker |
| Modifier and Type | Field and Description |
|---|---|
private static java.util.concurrent.atomic.AtomicInteger |
id |
(package private) static InternalLogger |
logger |
private int |
mask |
private static SharedResourceMisuseDetector |
misuseDetector |
private long |
startTime |
private java.util.concurrent.CountDownLatch |
startTimeInitialized |
private long |
tickDuration |
private java.util.Queue<HashedWheelTimer.HashedWheelTimeout> |
timeouts |
private HashedWheelTimer.HashedWheelBucket[] |
wheel |
private HashedWheelTimer.Worker |
worker |
static int |
WORKER_STATE_INIT |
static int |
WORKER_STATE_SHUTDOWN |
static int |
WORKER_STATE_STARTED |
private static java.util.concurrent.atomic.AtomicIntegerFieldUpdater<HashedWheelTimer> |
WORKER_STATE_UPDATER |
private int |
workerState |
private java.lang.Thread |
workerThread |
| Constructor and Description |
|---|
HashedWheelTimer()
Creates a new timer with the default thread factory
(
Executors.defaultThreadFactory()), default tick duration, and
default number of ticks per wheel. |
HashedWheelTimer(long tickDuration,
java.util.concurrent.TimeUnit unit)
Creates a new timer with the default thread factory
(
Executors.defaultThreadFactory()) and default number of ticks
per wheel. |
HashedWheelTimer(long tickDuration,
java.util.concurrent.TimeUnit unit,
int ticksPerWheel)
Creates a new timer with the default thread factory
(
Executors.defaultThreadFactory()). |
HashedWheelTimer(java.util.concurrent.ThreadFactory threadFactory)
Creates a new timer with the default tick duration and default number of
ticks per wheel.
|
HashedWheelTimer(java.util.concurrent.ThreadFactory threadFactory,
long tickDuration,
java.util.concurrent.TimeUnit unit)
Creates a new timer with the default number of ticks per wheel.
|
HashedWheelTimer(java.util.concurrent.ThreadFactory threadFactory,
long tickDuration,
java.util.concurrent.TimeUnit unit,
int ticksPerWheel)
Creates a new timer.
|
HashedWheelTimer(java.util.concurrent.ThreadFactory threadFactory,
ThreadNameDeterminer determiner,
long tickDuration,
java.util.concurrent.TimeUnit unit,
int ticksPerWheel)
Creates a new timer.
|
| Modifier and Type | Method and Description |
|---|---|
private static HashedWheelTimer.HashedWheelBucket[] |
createWheel(int ticksPerWheel) |
Timeout |
newTimeout(TimerTask task,
long delay,
java.util.concurrent.TimeUnit unit)
Schedules the specified
TimerTask for one-time execution after
the specified delay. |
private static int |
normalizeTicksPerWheel(int ticksPerWheel) |
void |
start()
Starts the background thread explicitly.
|
java.util.Set<Timeout> |
stop()
Releases all resources acquired by this
Timer and cancels all
tasks which were scheduled but not executed yet. |
static final InternalLogger logger
private static final java.util.concurrent.atomic.AtomicInteger id
private static final SharedResourceMisuseDetector misuseDetector
private static final java.util.concurrent.atomic.AtomicIntegerFieldUpdater<HashedWheelTimer> WORKER_STATE_UPDATER
private final HashedWheelTimer.Worker worker
private final java.lang.Thread workerThread
public static final int WORKER_STATE_INIT
public static final int WORKER_STATE_STARTED
public static final int WORKER_STATE_SHUTDOWN
private volatile int workerState
private final long tickDuration
private final HashedWheelTimer.HashedWheelBucket[] wheel
private final int mask
private final java.util.concurrent.CountDownLatch startTimeInitialized
private final java.util.Queue<HashedWheelTimer.HashedWheelTimeout> timeouts
private volatile long startTime
public HashedWheelTimer()
Executors.defaultThreadFactory()), default tick duration, and
default number of ticks per wheel.public HashedWheelTimer(long tickDuration,
java.util.concurrent.TimeUnit unit)
Executors.defaultThreadFactory()) and default number of ticks
per wheel.tickDuration - the duration between tickunit - the time unit of the tickDurationpublic HashedWheelTimer(long tickDuration,
java.util.concurrent.TimeUnit unit,
int ticksPerWheel)
Executors.defaultThreadFactory()).tickDuration - the duration between tickunit - the time unit of the tickDurationticksPerWheel - the size of the wheelpublic HashedWheelTimer(java.util.concurrent.ThreadFactory threadFactory)
threadFactory - a ThreadFactory that creates a
background Thread which is dedicated to
TimerTask execution.public HashedWheelTimer(java.util.concurrent.ThreadFactory threadFactory,
long tickDuration,
java.util.concurrent.TimeUnit unit)
threadFactory - a ThreadFactory that creates a
background Thread which is dedicated to
TimerTask execution.tickDuration - the duration between tickunit - the time unit of the tickDurationpublic HashedWheelTimer(java.util.concurrent.ThreadFactory threadFactory,
long tickDuration,
java.util.concurrent.TimeUnit unit,
int ticksPerWheel)
threadFactory - a ThreadFactory that creates a
background Thread which is dedicated to
TimerTask execution.tickDuration - the duration between tickunit - the time unit of the tickDurationticksPerWheel - the size of the wheelpublic HashedWheelTimer(java.util.concurrent.ThreadFactory threadFactory,
ThreadNameDeterminer determiner,
long tickDuration,
java.util.concurrent.TimeUnit unit,
int ticksPerWheel)
threadFactory - a ThreadFactory that creates a
background Thread which is dedicated to
TimerTask execution.determiner - thread name determiner to control thread name.tickDuration - the duration between tickunit - the time unit of the tickDurationticksPerWheel - the size of the wheelprivate static HashedWheelTimer.HashedWheelBucket[] createWheel(int ticksPerWheel)
private static int normalizeTicksPerWheel(int ticksPerWheel)
public void start()
java.lang.IllegalStateException - if this timer has been
stopped alreadypublic java.util.Set<Timeout> stop()
TimerTimer and cancels all
tasks which were scheduled but not executed yet.public Timeout newTimeout(TimerTask task, long delay, java.util.concurrent.TimeUnit unit)
TimerTimerTask for one-time execution after
the specified delay.newTimeout in interface Timer