public class TimeWheel {
/** 一个时间槽的时间 */
private long tickMs;
/** 时间轮大小 */
private int wheelSize;
/** 时间跨度 */
private long interval;
/** 槽 */
private Bucket[] buckets;
/** 时间轮指针 */
private long currentTimestamp;
/** 上层时间轮 */
private volatile TimeWheel overflowWheel;
public TimeWheel(long tickMs, int wheelSize, long currentTimestamp) {
this.currentTimestamp = currentTimestamp;
this.tickMs = tickMs;
this.wheelSize = wheelSize;
this.interval = tickMs * wheelSize;
this.buckets = new Bucket[wheelSize];
this.currentTimestamp = currentTimestamp - (currentTimestamp % tickMs);
for (int i = 0; i < wheelSize; i++) {
buckets[i] = new Bucket();
}
}
}
将任务添加到时间轮中十分简单,对于每个时间轮来说,比如说秒级时间轮,和分级时间轮,都有它自己的过期槽。也就是delayMs < tickMs的时候。</