HashedWheelTimer 主要用来高效处理大量定时任务, 且任务对时间精度要求相对不高, 比如链接超时管理等场景, 缺点是, 内存占用相对较高

//创建Timer, 精度为100毫秒
HashedWheelTimer timer = new HashedWheelTimer(100, TimeUnit.MILLISECONDS);
System.out.println(LocalTime.now());

timer.newTimeout(timeout -> {
     System.out.println(LocalTime.now());
     System.out.println(Thread.currentThread().getId());
},5,TimeUnit.SECONDS);

System.out.println(Thread.currentThread().getId());

构造函数

HashedWheelTimer(
    ThreadFactory threadFactory, //类似于Clock中的updater, 负责创建Worker线程.
    long tickDuration,           //时间刻度之间的时长(默认100ms), 通俗的说, 就是多久tick++一次.
    TimeUnit unit,               //tickDuration的单位.
    int ticksPerWheel            //类似于Clock中的wheel的长度(默认512).
):

转载于:https://my.oschina.net/u/256606/blog/758973