在学习kafka关于时间轮的实现的过程中,发现了两个困惑点:
1、为什么“currentTime”需要向下取整?如果不向下取整入,会有什么影响?代码显示如下:
@nonthreadsafe
private[timer] class TimingWheel(tickMs: Long, wheelSize: Int, startMs: Long, taskCounter: AtomicInteger, queue: DelayQueue[TimerTaskList]) {
private[this] var currentTime = startMs - (startMs % tickMs) // rounding down to multiple of tickMs
}
2、创建多层时间轮时,为什么这里的“startMs”字段是当前层“currentTime”字段的值(这个值已经向下取整,而不是原始的值)?代码显示如下:
@nonthreadsafe
private[timer] class TimingWheel(tickMs: Long, wheelSize: Int, startMs: Long, taskCounter: AtomicInteger, queue: DelayQueue[TimerTaskList]) {
private[this] var currentTime = startMs - (startMs % tickMs) // rounding down to multiple of tickMs
private[this] def addOverflowWheel(): Unit = {
synchronized {
if (overflowWheel == null) {
overflowWheel = new TimingWheel(
tickMs = interval,
wheelSize = wheelSize,
startMs = currentTime,
taskCounter = taskCounter,
queue
)
}
}
}
}
希望有大佬能帮忙解惑!!!
你的答案