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 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.
Bucket that stores HashedWheelTimeouts. |
| private static class | |
| private class |
| Modifier and Type | Field and Description |
|---|---|
| private final Queue | |
| private static final int | |
| private static final AtomicInteger | |
| private final ResourceLeakTracker | |
| private static final ResourceLeakDetector | |
| pack-priv static final InternalLogger | |
| private final int | |
| private final long | |
| private static final long | |
| private final AtomicLong | |
| private volatile long | |
| private final CountDownLatch | |
| private final Executor | |
| private final long | |
| private final Queue | |
| private static final AtomicBoolean | |
| private final HashedWheelTimer. | |
| private final HashedWheelTimer. | |
| public static final int | |
| public static final int | |
| public static final int | |
| private static final AtomicIntegerFieldUpdater | |
| private volatile int | |
| private final Thread |
| Access | Constructor and Description |
|---|---|
| public | HashedWheelTimer()
Creates a new timer with the default thread factory
( |
| public | HashedWheelTimer(long
the duration between tick tickDuration, TimeUnit the time unit of the unit)tickDurationCreates a new timer with the default thread factory
( |
| public | HashedWheelTimer(long
the duration between tick tickDuration, TimeUnit the time unit of the unit, int tickDurationthe size of the wheel ticksPerWheel)Creates a new timer with the default thread factory
( |
| public | HashedWheelTimer(ThreadFactory threadFactory)
Creates a new timer with the default tick duration and default number of ticks per wheel. |
| public | HashedWheelTimer(ThreadFactory threadFactory, long
the duration between tick tickDuration, TimeUnit the time unit of the unit)tickDurationCreates a new timer with the default number of ticks per wheel. |
| public | HashedWheelTimer(ThreadFactory threadFactory, long
the duration between tick tickDuration, TimeUnit the time unit of the unit, int tickDurationthe size of the wheel ticksPerWheel)Creates a new timer. |
| public | HashedWheelTimer(ThreadFactory threadFactory, long
the duration between tick tickDuration, TimeUnit the time unit of the unit, int tickDurationthe size of the wheel ticksPerWheel, boolean true if leak detection should be enabled always,
if false it will only be enabled if the worker thread is not
a daemon thread.Creates a new timer. |
| public | HashedWheelTimer(ThreadFactory threadFactory, long
the duration between tick tickDuration, TimeUnit the time unit of the unit, int tickDurationthe size of the wheel ticksPerWheel, boolean true if leak detection should be enabled always,
if false it will only be enabled if the worker thread is not
a daemon thread.The maximum number of pending timeouts after which call to
maxPendingTimeouts)newTimeout will result in
java.
being thrown. No maximum pending timeouts limit is assumed if
this value is 0 or negative.Creates a new timer. |
| public | HashedWheelTimer(ThreadFactory threadFactory, long
the duration between tick tickDuration, TimeUnit the time unit of the unit, int tickDurationthe size of the wheel ticksPerWheel, boolean true if leak detection should be enabled always,
if false it will only be enabled if the worker thread is not
a daemon thread.The maximum number of pending timeouts after which call to
maxPendingTimeouts, Executor taskExecutor)newTimeout will result in
java.
being thrown. No maximum pending timeouts limit is assumed if
this value is 0 or negative.Creates a new timer. |
| Modifier and Type | Method and Description |
|---|---|
| private static HashedWheelTimer. | |
| protected void | finalize()
Overrides java. |
| public Timeout | newTimeout(TimerTask task, long delay, TimeUnit unit)
Implements io. TimerTask for one-time execution after
the specified delay.
|
| private static int | |
| public long | |
| private static void | |
| public void | |
| public Set | stop()
Implements io. Timer and cancels all
tasks which were scheduled but not executed yet.
|
| cancelledTimeouts | back to summary |
|---|---|
| private final Queue<HashedWheelTimer. | |
| INSTANCE_COUNT_LIMIT | back to summary |
|---|---|
| private static final int INSTANCE_COUNT_LIMIT | |
| INSTANCE_COUNTER | back to summary |
|---|---|
| private static final AtomicInteger INSTANCE_COUNTER | |
| leak | back to summary |
|---|---|
| private final ResourceLeakTracker<HashedWheelTimer> leak | |
| leakDetector | back to summary |
|---|---|
| private static final ResourceLeakDetector<HashedWheelTimer> leakDetector | |
| logger | back to summary |
|---|---|
| pack-priv static final InternalLogger logger | |
| mask | back to summary |
|---|---|
| private final int mask | |
| maxPendingTimeouts | back to summary |
|---|---|
| private final long maxPendingTimeouts | |
| MILLISECOND_NANOS | back to summary |
|---|---|
| private static final long MILLISECOND_NANOS | |
| pendingTimeouts | back to summary |
|---|---|
| private final AtomicLong pendingTimeouts | |
| startTime | back to summary |
|---|---|
| private volatile long startTime | |
| startTimeInitialized | back to summary |
|---|---|
| private final CountDownLatch startTimeInitialized | |
| taskExecutor | back to summary |
|---|---|
| private final Executor taskExecutor | |
| tickDuration | back to summary |
|---|---|
| private final long tickDuration | |
| timeouts | back to summary |
|---|---|
| private final Queue<HashedWheelTimer. | |
| WARNED_TOO_MANY_INSTANCES | back to summary |
|---|---|
| private static final AtomicBoolean WARNED_TOO_MANY_INSTANCES | |
| wheel | back to summary |
|---|---|
| private final HashedWheelTimer. | |
| worker | back to summary |
|---|---|
| private final HashedWheelTimer. | |
| WORKER_STATE_INIT | back to summary |
|---|---|
| public static final int WORKER_STATE_INIT | |
| WORKER_STATE_SHUTDOWN | back to summary |
|---|---|
| public static final int WORKER_STATE_SHUTDOWN | |
| WORKER_STATE_STARTED | back to summary |
|---|---|
| public static final int WORKER_STATE_STARTED | |
| WORKER_STATE_UPDATER | back to summary |
|---|---|
| private static final AtomicIntegerFieldUpdater<HashedWheelTimer> WORKER_STATE_UPDATER | |
| workerState | back to summary |
|---|---|
private volatile int workerState
| |
| workerThread | back to summary |
|---|---|
| private final Thread workerThread | |
| HashedWheelTimer | back to summary |
|---|---|
| public HashedWheelTimer() Creates a new timer with the default thread factory
( | |
| HashedWheelTimer | back to summary |
|---|---|
| public HashedWheelTimer(long tickDuration, TimeUnit unit) Creates a new timer with the default thread factory
(
| |
| HashedWheelTimer | back to summary |
|---|---|
| public HashedWheelTimer(long tickDuration, TimeUnit unit, int ticksPerWheel) Creates a new timer with the default thread factory
(
| |
| HashedWheelTimer | back to summary |
|---|---|
| public HashedWheelTimer(ThreadFactory threadFactory) Creates a new timer with the default tick duration and default number of ticks per wheel.
| |
| HashedWheelTimer | back to summary |
|---|---|
| public HashedWheelTimer(ThreadFactory threadFactory, long tickDuration, TimeUnit unit) Creates a new timer with the default number of ticks per wheel.
| |
| HashedWheelTimer | back to summary |
|---|---|
| public HashedWheelTimer(ThreadFactory threadFactory, long tickDuration, TimeUnit unit, int ticksPerWheel) Creates a new timer.
| |
| HashedWheelTimer | back to summary |
|---|---|
| public HashedWheelTimer(ThreadFactory threadFactory, long tickDuration, TimeUnit unit, int ticksPerWheel, boolean leakDetection) Creates a new timer.
| |
| HashedWheelTimer | back to summary |
|---|---|
| public HashedWheelTimer(ThreadFactory threadFactory, long tickDuration, TimeUnit unit, int ticksPerWheel, boolean leakDetection, long maxPendingTimeouts) Creates a new timer.
| |
| HashedWheelTimer | back to summary |
|---|---|
| public HashedWheelTimer(ThreadFactory threadFactory, long tickDuration, TimeUnit unit, int ticksPerWheel, boolean leakDetection, long maxPendingTimeouts, Executor taskExecutor) Creates a new timer.
| |
| createWheel | back to summary |
|---|---|
| private static HashedWheelTimer. | |
| finalize | back to summary |
|---|---|
| protected void finalize() throws Throwable Overrides java. Doc from java. Called by the garbage collector on an object when garbage collection
determines that there are no more references to the object.
A subclass overrides the
When running in a Java virtual machine in which finalization has been
disabled or removed, the garbage collector will never call
The general contract of
The
The Java programming language does not guarantee which thread will
invoke the
After the
The
Any exception thrown by the | |
| newTimeout | back to summary |
|---|---|
| public Timeout newTimeout(TimerTask task, long delay, TimeUnit unit) Implements io. Doc from io. Schedules the specified | |
| normalizeTicksPerWheel | back to summary |
|---|---|
| private static int normalizeTicksPerWheel(int ticksPerWheel) | |
| pendingTimeouts | back to summary |
|---|---|
| public long pendingTimeouts() Returns the number of pending timeouts of this | |
| reportTooManyInstances | back to summary |
|---|---|
| private static void reportTooManyInstances() | |
| start | back to summary |
|---|---|
| public void start() Starts the background thread explicitly. The background thread will start automatically on demand even if you did not call this method.
| |
| stop | back to summary |
|---|---|
| public Set Implements io. Doc from io. Releases all resources acquired by this | |
| Modifier and Type | Field and Description |
|---|---|
| private HashedWheelTimer. | |
| private HashedWheelTimer. |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public void | |
| public void | |
| public void | |
| private HashedWheelTimer. | |
| public HashedWheelTimer. |
| head | back to summary |
|---|---|
| private HashedWheelTimer. | |
| tail | back to summary |
|---|---|
| private HashedWheelTimer. | |
| HashedWheelBucket | back to summary |
|---|---|
| private HashedWheelBucket() | |
| addTimeout | back to summary |
|---|---|
| public void addTimeout(HashedWheelTimer. Add | |
| clearTimeouts | back to summary |
|---|---|
| public void clearTimeouts(Set<Timeout> set) Clear this bucket and return all not expired / cancelled | |
| expireTimeouts | back to summary |
|---|---|
| public void expireTimeouts(long deadline) Expire all | |
| pollTimeout | back to summary |
|---|---|
| private HashedWheelTimer. | |
| remove | back to summary |
|---|---|
| public HashedWheelTimer. | |
| Modifier and Type | Field and Description |
|---|---|
| pack-priv HashedWheelTimer. | |
| private final long | |
| pack-priv HashedWheelTimer. | |
| pack-priv HashedWheelTimer. | |
| pack-priv long | |
| private static final int | |
| private static final int | |
| private static final int | |
| private volatile int | |
| private static final AtomicIntegerFieldUpdater | |
| private final TimerTask | |
| private final HashedWheelTimer |
| Access | Constructor and Description |
|---|---|
| pack-priv |
| Modifier and Type | Method and Description |
|---|---|
| public boolean | cancel()
Implements io. TimerTask associated with this handle.
|
| public boolean | |
| public void | |
| public boolean | isCancelled()
Implements io. true if and only if the TimerTask associated
with this handle has been cancelled.
|
| public boolean | isExpired()
Implements io. true if and only if the TimerTask associated
with this handle has been expired.
|
| pack-priv void | |
| public void | |
| public int | |
| public TimerTask | task()
Implements io. TimerTask which is associated with this handle.
|
| public Timer | |
| public String |
| bucket | back to summary |
|---|---|
| pack-priv HashedWheelTimer. | |
| deadline | back to summary |
|---|---|
| private final long deadline | |
| next | back to summary |
|---|---|
| pack-priv HashedWheelTimer. | |
| prev | back to summary |
|---|---|
| pack-priv HashedWheelTimer. | |
| remainingRounds | back to summary |
|---|---|
| pack-priv long remainingRounds | |
| ST_CANCELLED | back to summary |
|---|---|
| private static final int ST_CANCELLED | |
| ST_EXPIRED | back to summary |
|---|---|
| private static final int ST_EXPIRED | |
| ST_INIT | back to summary |
|---|---|
| private static final int ST_INIT | |
| state | back to summary |
|---|---|
private volatile int state
| |
| STATE_UPDATER | back to summary |
|---|---|
| private static final AtomicIntegerFieldUpdater<HashedWheelTimer. | |
| task | back to summary |
|---|---|
| private final TimerTask task | |
| timer | back to summary |
|---|---|
| private final HashedWheelTimer timer | |
| HashedWheelTimeout | back to summary |
|---|---|
| pack-priv HashedWheelTimeout(HashedWheelTimer timer, TimerTask task, long deadline) | |
| cancel | back to summary |
|---|---|
| public boolean cancel() Implements io. Doc from io. Attempts to cancel the
| |
| compareAndSetState | back to summary |
|---|---|
| public boolean compareAndSetState(int expected, int state) | |
| expire | back to summary |
|---|---|
| public void expire() | |
| isCancelled | back to summary |
|---|---|
| public boolean isCancelled() Implements io. Doc from io. Returns
| |
| isExpired | back to summary |
|---|---|
| public boolean isExpired() Implements io. Doc from io. Returns
| |
| remove | back to summary |
|---|---|
| pack-priv void remove() | |
| run | back to summary |
|---|---|
| public void run() Implements java. Doc from java. Runs this operation.
| |
| state | back to summary |
|---|---|
| public int state() | |
| task | back to summary |
|---|---|
| public TimerTask task() Implements io. Doc from io. Returns the
| |
| timer | back to summary |
|---|---|
| public Timer timer() Implements io. Doc from io. Returns the
| |
| toString | back to summary |
|---|---|
| public String toString() Overrides java. Doc from java. Returns a string representation of the object. | |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| private void | |
| public void | |
| private void | |
| public Set | |
| private long | Returns: Long.MIN_VALUE if received a shutdown request, current time otherwise (with Long.MIN_VALUE changed by +1)calculate goal nanoTime from startTime and current tick number, then wait until that goal has been reached. |
| tick | back to summary |
|---|---|
| private long tick | |
| unprocessedTimeouts | back to summary |
|---|---|
| private final Set<Timeout> unprocessedTimeouts | |
| Worker | back to summary |
|---|---|
| private Worker() | |
| processCancelledTasks | back to summary |
|---|---|
| private void processCancelledTasks() | |
| run | back to summary |
|---|---|
| public void run() Implements java. Doc from java. Runs this operation.
| |
| transferTimeoutsToBuckets | back to summary |
|---|---|
| private void transferTimeoutsToBuckets() | |
| unprocessedTimeouts | back to summary |
|---|---|
| public Set | |
| waitForNextTick | back to summary |
|---|---|
| private long waitForNextTick() calculate goal nanoTime from startTime and current tick number, then wait until that goal has been reached.
| |