public class HashedWheelTimer extends Object implements Timer
Timer optimized for approximated I/O timeout scheduling.
As described with 'approximated', this timer does not execute the scheduled
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.| 构造器和说明 |
|---|
HashedWheelTimer(ThreadFactory threadFactory)
初始化一个环形时间执行器.
|
HashedWheelTimer(ThreadFactory threadFactory,
long tickDuration,
TimeUnit unit,
int ticksPerWheel)
初始化一个环形时间执行器.
|
| 限定符和类型 | 方法和说明 |
|---|---|
boolean |
isStop()
Is stop boolean.
|
Timeout |
newTimeout(TimerTask task,
long delay,
TimeUnit unit)
Schedules the specified
TimerTask for one-time execution after
the specified delay. |
void |
start()
启动任务.
|
Set<Timeout> |
stop()
Releases all resources acquired by this
Timer and cancels all
tasks which were scheduled but not executed yet. |
public HashedWheelTimer(ThreadFactory threadFactory)
threadFactory - 线程初始化工厂;public HashedWheelTimer(ThreadFactory threadFactory, long tickDuration, TimeUnit unit, int ticksPerWheel)
threadFactory - 线程初始化工厂;tickDuration - 持续执行时间;unit - 时间单位;ticksPerWheel - 时间分片数;public void start()
public Timeout newTimeout(TimerTask task, long delay, TimeUnit unit)
TimerTimerTask for one-time execution after
the specified delay.newTimeout 在接口中 Timertask - timeTaskdelay - delay time;unit - time unit;public Set<Timeout> stop()
TimerTimer and cancels all
tasks which were scheduled but not executed yet.Copyright © 2022 dromara. All rights reserved.