类 HashedWheelTimer
- java.lang.Object
-
- org.apache.dubbo.common.timer.HashedWheelTimer
-
- 所有已实现的接口:
Timer
public class HashedWheelTimer extends Object implements Timer
ATimeroptimized for approximated I/O timeout scheduling.Tick Duration
As described with 'approximated', this timer does not execute the scheduled
TimerTaskon 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 Details
HashedWheelTimeris 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.
-
-
构造器概要
构造器 构造器 说明 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, long maxPendingTimeouts)Creates a new timer.
-
方法概要
所有方法 实例方法 具体方法 修饰符和类型 方法 说明 booleanisStop()the timer is stopTimeoutnewTimeout(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.
-
-
-
构造器详细资料
-
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
public HashedWheelTimer(long tickDuration, TimeUnit unit)Creates a new timer with the default thread factory (Executors.defaultThreadFactory()) and default number of ticks per wheel.- 参数:
tickDuration- the duration between tickunit- the time unit of thetickDuration- 抛出:
NullPointerException- ifunitisnullIllegalArgumentException- iftickDurationis <= 0
-
HashedWheelTimer
public HashedWheelTimer(long tickDuration, TimeUnit unit, int ticksPerWheel)Creates a new timer with the default thread factory (Executors.defaultThreadFactory()).- 参数:
tickDuration- the duration between tickunit- the time unit of thetickDurationticksPerWheel- the size of the wheel- 抛出:
NullPointerException- ifunitisnullIllegalArgumentException- if either oftickDurationandticksPerWheelis <= 0
-
HashedWheelTimer
public HashedWheelTimer(ThreadFactory threadFactory)
Creates a new timer with the default tick duration and default number of ticks per wheel.- 参数:
threadFactory- aThreadFactorythat creates a backgroundThreadwhich is dedicated toTimerTaskexecution.- 抛出:
NullPointerException- ifthreadFactoryisnull
-
HashedWheelTimer
public HashedWheelTimer(ThreadFactory threadFactory, long tickDuration, TimeUnit unit)
Creates a new timer with the default number of ticks per wheel.- 参数:
threadFactory- aThreadFactorythat creates a backgroundThreadwhich is dedicated toTimerTaskexecution.tickDuration- the duration between tickunit- the time unit of thetickDuration- 抛出:
NullPointerException- if either ofthreadFactoryandunitisnullIllegalArgumentException- iftickDurationis <= 0
-
HashedWheelTimer
public HashedWheelTimer(ThreadFactory threadFactory, long tickDuration, TimeUnit unit, int ticksPerWheel)
Creates a new timer.- 参数:
threadFactory- aThreadFactorythat creates a backgroundThreadwhich is dedicated toTimerTaskexecution.tickDuration- the duration between tickunit- the time unit of thetickDurationticksPerWheel- the size of the wheel- 抛出:
NullPointerException- if either ofthreadFactoryandunitisnullIllegalArgumentException- if either oftickDurationandticksPerWheelis <= 0
-
HashedWheelTimer
public HashedWheelTimer(ThreadFactory threadFactory, long tickDuration, TimeUnit unit, int ticksPerWheel, long maxPendingTimeouts)
Creates a new timer.- 参数:
threadFactory- aThreadFactorythat creates a backgroundThreadwhich is dedicated toTimerTaskexecution.tickDuration- the duration between tickunit- the time unit of thetickDurationticksPerWheel- the size of the wheelmaxPendingTimeouts- The maximum number of pending timeouts after which call tonewTimeoutwill result inRejectedExecutionExceptionbeing thrown. No maximum pending timeouts limit is assumed if this value is 0 or negative.- 抛出:
NullPointerException- if either ofthreadFactoryandunitisnullIllegalArgumentException- if either oftickDurationandticksPerWheelis <= 0
-
-
方法详细资料
-
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.- 抛出:
IllegalStateException- if this timer has been stopped already
-
stop
public Set<Timeout> stop()
从接口复制的说明:TimerReleases all resources acquired by thisTimerand cancels all tasks which were scheduled but not executed yet.
-
isStop
public boolean isStop()
从接口复制的说明:Timerthe timer is stop
-
newTimeout
public Timeout newTimeout(TimerTask task, long delay, TimeUnit unit)
从接口复制的说明:TimerSchedules the specifiedTimerTaskfor one-time execution after the specified delay.- 指定者:
newTimeout在接口中Timer- 返回:
- a handle which is associated with the specified task
-
pendingTimeouts
public long pendingTimeouts()
Returns the number of pending timeouts of thisTimer.
-
-