- java.lang.Object
- 
- io.netty5.util.HashedWheelTimer
 
- 
- All Implemented Interfaces:
- Timer
 
 public class HashedWheelTimer extends Object implements Timer ATimeroptimized for approximated I/O timeout scheduling.Tick DurationAs described with 'approximated', this timer does not execute the scheduledTimerTaskon time.HashedWheelTimer, on every tick, will check if there are anyTimerTasks 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)HashedWheelTimermaintains a data structure called 'wheel'. To put simply, a wheel is a hash table ofTimerTasks 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.HashedWheelTimercreates 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 DetailsHashedWheelTimeris 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.
- 
- 
Field SummaryFields Modifier and Type Field Description static intWORKER_STATE_INITstatic intWORKER_STATE_SHUTDOWNstatic intWORKER_STATE_STARTED
 - 
Constructor SummaryConstructors Constructor 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, 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 SummaryAll Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voidfinalize()TimeoutnewTimeout(TimerTask task, long delay, TimeUnit unit)Schedules the specifiedTimerTaskfor one-time execution after the specified delay.longpendingTimeouts()Returns the number of pending timeouts of thisTimer.voidstart()Starts the background thread explicitly.Set<Timeout>stop()Releases all resources acquired by thisTimerand cancels all tasks which were scheduled but not executed yet.
 
- 
- 
- 
Field Detail- 
WORKER_STATE_INITpublic static final int WORKER_STATE_INIT - See Also:
- Constant Field Values
 
 - 
WORKER_STATE_STARTEDpublic static final int WORKER_STATE_STARTED - See Also:
- Constant Field Values
 
 - 
WORKER_STATE_SHUTDOWNpublic static final int WORKER_STATE_SHUTDOWN - See Also:
- Constant Field Values
 
 
- 
 - 
Constructor Detail- 
HashedWheelTimerpublic HashedWheelTimer() Creates a new timer with the default thread factory (Executors.defaultThreadFactory()), default tick duration, and default number of ticks per wheel.
 - 
HashedWheelTimerpublic HashedWheelTimer(long tickDuration, TimeUnit unit)Creates a new timer with the default thread factory (Executors.defaultThreadFactory()) and default number of ticks per wheel.- Parameters:
- tickDuration- the duration between tick
- unit- the time unit of the- tickDuration
- Throws:
- NullPointerException- if- unitis- null
- IllegalArgumentException- if- tickDurationis <= 0
 
 - 
HashedWheelTimerpublic HashedWheelTimer(long tickDuration, TimeUnit unit, int ticksPerWheel)Creates a new timer with the default thread factory (Executors.defaultThreadFactory()).- Parameters:
- tickDuration- the duration between tick
- unit- the time unit of the- tickDuration
- ticksPerWheel- the size of the wheel
- Throws:
- NullPointerException- if- unitis- null
- IllegalArgumentException- if either of- tickDurationand- ticksPerWheelis <= 0
 
 - 
HashedWheelTimerpublic HashedWheelTimer(ThreadFactory threadFactory) Creates a new timer with the default tick duration and default number of ticks per wheel.- Parameters:
- threadFactory- a- ThreadFactorythat creates a background- Threadwhich is dedicated to- TimerTaskexecution.
- Throws:
- NullPointerException- if- threadFactoryis- null
 
 - 
HashedWheelTimerpublic HashedWheelTimer(ThreadFactory threadFactory, long tickDuration, TimeUnit unit) Creates a new timer with the default number of ticks per wheel.- Parameters:
- threadFactory- a- ThreadFactorythat creates a background- Threadwhich is dedicated to- TimerTaskexecution.
- tickDuration- the duration between tick
- unit- the time unit of the- tickDuration
- Throws:
- NullPointerException- if either of- threadFactoryand- unitis- null
- IllegalArgumentException- if- tickDurationis <= 0
 
 - 
HashedWheelTimerpublic HashedWheelTimer(ThreadFactory threadFactory, long tickDuration, TimeUnit unit, int ticksPerWheel) Creates a new timer.- Parameters:
- threadFactory- a- ThreadFactorythat creates a background- Threadwhich is dedicated to- TimerTaskexecution.
- tickDuration- the duration between tick
- unit- the time unit of the- tickDuration
- ticksPerWheel- the size of the wheel
- Throws:
- NullPointerException- if either of- threadFactoryand- unitis- null
- IllegalArgumentException- if either of- tickDurationand- ticksPerWheelis <= 0
 
 - 
HashedWheelTimerpublic HashedWheelTimer(ThreadFactory threadFactory, long tickDuration, TimeUnit unit, int ticksPerWheel, boolean leakDetection) Creates a new timer.- Parameters:
- threadFactory- a- ThreadFactorythat creates a background- Threadwhich is dedicated to- TimerTaskexecution.
- tickDuration- the duration between tick
- unit- the time unit of the- tickDuration
- ticksPerWheel- the size of the wheel
- leakDetection-- trueif 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 of- threadFactoryand- unitis- null
- IllegalArgumentException- if either of- tickDurationand- ticksPerWheelis <= 0
 
 - 
HashedWheelTimerpublic HashedWheelTimer(ThreadFactory threadFactory, long tickDuration, TimeUnit unit, int ticksPerWheel, boolean leakDetection, long maxPendingTimeouts) Creates a new timer.- Parameters:
- threadFactory- a- ThreadFactorythat creates a background- Threadwhich is dedicated to- TimerTaskexecution.
- tickDuration- the duration between tick
- unit- the time unit of the- tickDuration
- ticksPerWheel- the size of the wheel
- leakDetection-- trueif 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 to- newTimeoutwill result in- RejectedExecutionExceptionbeing thrown. No maximum pending timeouts limit is assumed if this value is 0 or negative.
- Throws:
- NullPointerException- if either of- threadFactoryand- unitis- null
- IllegalArgumentException- if either of- tickDurationand- ticksPerWheelis <= 0
 
 - 
HashedWheelTimerpublic HashedWheelTimer(ThreadFactory threadFactory, long tickDuration, TimeUnit unit, int ticksPerWheel, boolean leakDetection, long maxPendingTimeouts, Executor taskExecutor) Creates a new timer.- Parameters:
- threadFactory- a- ThreadFactorythat creates a background- Threadwhich is dedicated to- TimerTaskexecution.
- tickDuration- the duration between tick
- unit- the time unit of the- tickDuration
- ticksPerWheel- the size of the wheel
- leakDetection-- trueif 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 to- newTimeoutwill result in- RejectedExecutionExceptionbeing thrown. No maximum pending timeouts limit is assumed if this value is 0 or negative.
- taskExecutor- The- Executorthat is used to execute the submitted- TimerTasks. The caller is responsible to shutdown the- Executoronce it is not needed anymore.
- Throws:
- NullPointerException- if either of- threadFactoryand- unitis- null
- IllegalArgumentException- if either of- tickDurationand- ticksPerWheelis <= 0
 
 
- 
 - 
Method Detail- 
finalizeprotected void finalize() throws Throwable
 - 
startpublic 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
 
 - 
stoppublic Set<Timeout> stop() Description copied from interface:TimerReleases all resources acquired by thisTimerand cancels all tasks which were scheduled but not executed yet.
 - 
newTimeoutpublic Timeout newTimeout(TimerTask task, long delay, TimeUnit unit) Description copied from interface:TimerSchedules the specifiedTimerTaskfor one-time execution after the specified delay.- Specified by:
- newTimeoutin interface- Timer
- Returns:
- a handle which is associated with the specified task
 
 - 
pendingTimeoutspublic long pendingTimeouts() Returns the number of pending timeouts of thisTimer.
 
- 
 
-