Package io.netty.util
Class HashedWheelTimer
java.lang.Object
io.netty.util.HashedWheelTimer
- All Implemented Interfaces:
Timer
A
Timer
optimized for approximated I/O timeout scheduling.
Tick Duration
As described with 'approximated', this timer does not execute the scheduledTimerTask
on time. HashedWheelTimer
, on every tick, will
check if there are any TimerTask
s 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.
Ticks per Wheel (Wheel Size)
HashedWheelTimer
maintains a data structure called 'wheel'.
To put simply, a wheel is a hash table of TimerTask
s 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.
Do not create many instances.
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.
Implementation Details
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.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static final class
Bucket that stores HashedWheelTimeouts.private static final class
private final class
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final Queue
<HashedWheelTimer.HashedWheelTimeout> private static final int
private static final AtomicInteger
private final ResourceLeakTracker
<HashedWheelTimer> private static final ResourceLeakDetector
<HashedWheelTimer> (package private) static final InternalLogger
private final int
private final long
private static final long
private final AtomicLong
private long
private final CountDownLatch
private final Executor
private final long
private final Queue
<HashedWheelTimer.HashedWheelTimeout> private static final AtomicBoolean
private final HashedWheelTimer.HashedWheelBucket[]
private final HashedWheelTimer.Worker
static final int
static final int
static final int
private static final AtomicIntegerFieldUpdater
<HashedWheelTimer> private int
private final Thread
-
Constructor Summary
ConstructorsConstructorDescriptionCreates a new timer with the default thread factory (Executors.defaultThreadFactory()
), default tick duration, and default number of ticks per wheel.HashedWheelTimer
(long tickDuration, TimeUnit unit) Creates a new timer with the default thread factory (Executors.defaultThreadFactory()
) and default number of ticks per wheel.HashedWheelTimer
(long tickDuration, TimeUnit unit, int ticksPerWheel) Creates a new timer with the default thread factory (Executors.defaultThreadFactory()
).HashedWheelTimer
(ThreadFactory threadFactory) Creates a new timer with the default tick duration and default number of ticks per wheel.HashedWheelTimer
(ThreadFactory threadFactory, long tickDuration, TimeUnit unit) Creates a new timer with the default number of ticks per wheel.HashedWheelTimer
(ThreadFactory threadFactory, long tickDuration, TimeUnit unit, int ticksPerWheel) Creates a new timer.HashedWheelTimer
(ThreadFactory threadFactory, long tickDuration, TimeUnit unit, int ticksPerWheel, boolean leakDetection) Creates a new timer.HashedWheelTimer
(ThreadFactory threadFactory, long tickDuration, TimeUnit unit, int ticksPerWheel, boolean leakDetection, long maxPendingTimeouts) Creates a new timer.HashedWheelTimer
(ThreadFactory threadFactory, long tickDuration, TimeUnit unit, int ticksPerWheel, boolean leakDetection, long maxPendingTimeouts, Executor taskExecutor) Creates a new timer. -
Method Summary
Modifier and TypeMethodDescriptionprivate static HashedWheelTimer.HashedWheelBucket[]
createWheel
(int ticksPerWheel) protected void
finalize()
newTimeout
(TimerTask task, long delay, TimeUnit unit) Schedules the specifiedTimerTask
for one-time execution after the specified delay.long
Returns the number of pending timeouts of thisTimer
.private static void
void
start()
Starts the background thread explicitly.stop()
Releases all resources acquired by thisTimer
and cancels all tasks which were scheduled but not executed yet.
-
Field Details
-
logger
-
INSTANCE_COUNTER
-
WARNED_TOO_MANY_INSTANCES
-
INSTANCE_COUNT_LIMIT
private static final int INSTANCE_COUNT_LIMIT- See Also:
-
MILLISECOND_NANOS
private static final long MILLISECOND_NANOS -
leakDetector
-
WORKER_STATE_UPDATER
-
leak
-
worker
-
workerThread
-
WORKER_STATE_INIT
public static final int WORKER_STATE_INIT- See Also:
-
WORKER_STATE_STARTED
public static final int WORKER_STATE_STARTED- See Also:
-
WORKER_STATE_SHUTDOWN
public static final int WORKER_STATE_SHUTDOWN- See Also:
-
workerState
private volatile int workerState -
tickDuration
private final long tickDuration -
wheel
-
mask
private final int mask -
startTimeInitialized
-
timeouts
-
cancelledTimeouts
-
pendingTimeouts
-
maxPendingTimeouts
private final long maxPendingTimeouts -
taskExecutor
-
startTime
private volatile long startTime
-
-
Constructor Details
-
HashedWheelTimer
public HashedWheelTimer()Creates a new timer with the default thread factory (Executors.defaultThreadFactory()
), default tick duration, and default number of ticks per wheel. -
HashedWheelTimer
Creates a new timer with the default thread factory (Executors.defaultThreadFactory()
) and default number of ticks per wheel.- Parameters:
tickDuration
- the duration between tickunit
- the time unit of thetickDuration
- Throws:
NullPointerException
- ifunit
isnull
IllegalArgumentException
- iftickDuration
is <= 0
-
HashedWheelTimer
Creates a new timer with the default thread factory (Executors.defaultThreadFactory()
).- Parameters:
tickDuration
- the duration between tickunit
- the time unit of thetickDuration
ticksPerWheel
- the size of the wheel- Throws:
NullPointerException
- ifunit
isnull
IllegalArgumentException
- if either oftickDuration
andticksPerWheel
is <= 0
-
HashedWheelTimer
Creates a new timer with the default tick duration and default number of ticks per wheel.- Parameters:
threadFactory
- aThreadFactory
that creates a backgroundThread
which is dedicated toTimerTask
execution.- Throws:
NullPointerException
- ifthreadFactory
isnull
-
HashedWheelTimer
Creates a new timer with the default number of ticks per wheel.- Parameters:
threadFactory
- aThreadFactory
that creates a backgroundThread
which is dedicated toTimerTask
execution.tickDuration
- the duration between tickunit
- the time unit of thetickDuration
- Throws:
NullPointerException
- if either ofthreadFactory
andunit
isnull
IllegalArgumentException
- iftickDuration
is <= 0
-
HashedWheelTimer
public HashedWheelTimer(ThreadFactory threadFactory, long tickDuration, TimeUnit unit, int ticksPerWheel) Creates a new timer.- Parameters:
threadFactory
- aThreadFactory
that creates a backgroundThread
which is dedicated toTimerTask
execution.tickDuration
- the duration between tickunit
- the time unit of thetickDuration
ticksPerWheel
- the size of the wheel- Throws:
NullPointerException
- if either ofthreadFactory
andunit
isnull
IllegalArgumentException
- if either oftickDuration
andticksPerWheel
is <= 0
-
HashedWheelTimer
public HashedWheelTimer(ThreadFactory threadFactory, long tickDuration, TimeUnit unit, int ticksPerWheel, boolean leakDetection) Creates a new timer.- Parameters:
threadFactory
- aThreadFactory
that creates a backgroundThread
which is dedicated toTimerTask
execution.tickDuration
- the duration between tickunit
- the time unit of thetickDuration
ticksPerWheel
- the size of the wheelleakDetection
-true
if leak detection should be enabled always, if false it will only be enabled if the worker thread is not a daemon thread.- Throws:
NullPointerException
- if either ofthreadFactory
andunit
isnull
IllegalArgumentException
- if either oftickDuration
andticksPerWheel
is <= 0
-
HashedWheelTimer
public HashedWheelTimer(ThreadFactory threadFactory, long tickDuration, TimeUnit unit, int ticksPerWheel, boolean leakDetection, long maxPendingTimeouts) Creates a new timer.- Parameters:
threadFactory
- aThreadFactory
that creates a backgroundThread
which is dedicated toTimerTask
execution.tickDuration
- the duration between tickunit
- the time unit of thetickDuration
ticksPerWheel
- the size of the wheelleakDetection
-true
if leak detection should be enabled always, if false it will only be enabled if the worker thread is not a daemon thread.maxPendingTimeouts
- The maximum number of pending timeouts after which call tonewTimeout
will result inRejectedExecutionException
being thrown. No maximum pending timeouts limit is assumed if this value is 0 or negative.- Throws:
NullPointerException
- if either ofthreadFactory
andunit
isnull
IllegalArgumentException
- if either oftickDuration
andticksPerWheel
is <= 0
-
HashedWheelTimer
public HashedWheelTimer(ThreadFactory threadFactory, long tickDuration, TimeUnit unit, int ticksPerWheel, boolean leakDetection, long maxPendingTimeouts, Executor taskExecutor) Creates a new timer.- Parameters:
threadFactory
- aThreadFactory
that creates a backgroundThread
which is dedicated toTimerTask
execution.tickDuration
- the duration between tickunit
- the time unit of thetickDuration
ticksPerWheel
- the size of the wheelleakDetection
-true
if leak detection should be enabled always, if false it will only be enabled if the worker thread is not a daemon thread.maxPendingTimeouts
- The maximum number of pending timeouts after which call tonewTimeout
will result inRejectedExecutionException
being thrown. No maximum pending timeouts limit is assumed if this value is 0 or negative.taskExecutor
- TheExecutor
that is used to execute the submittedTimerTask
s. The caller is responsible to shutdown theExecutor
once it is not needed anymore.- Throws:
NullPointerException
- if either ofthreadFactory
andunit
isnull
IllegalArgumentException
- if either oftickDuration
andticksPerWheel
is <= 0
-
-
Method Details
-
finalize
-
createWheel
-
start
public void start()Starts the background thread explicitly. The background thread will start automatically on demand even if you did not call this method.- Throws:
IllegalStateException
- if this timer has been stopped already
-
stop
Description copied from interface:Timer
Releases all resources acquired by thisTimer
and cancels all tasks which were scheduled but not executed yet. -
newTimeout
Description copied from interface:Timer
Schedules the specifiedTimerTask
for one-time execution after the specified delay.- Specified by:
newTimeout
in interfaceTimer
- Returns:
- a handle which is associated with the specified task
-
pendingTimeouts
public long pendingTimeouts()Returns the number of pending timeouts of thisTimer
. -
reportTooManyInstances
private static void reportTooManyInstances()
-