From 25e1ee4d629ef96fded3e4bd0d8ca4250d235b96 Mon Sep 17 00:00:00 2001 From: BeauFang Date: Sat, 5 Nov 2022 12:37:26 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=97=B6=E9=97=B4=E8=BD=AE=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E4=BB=BB=E5=8A=A1=E7=BA=BF=E7=A8=8B=E5=AE=89=E5=85=A8?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cn/hutool/cron/timingwheel/TimingWheel.java | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/hutool-cron/src/main/java/cn/hutool/cron/timingwheel/TimingWheel.java b/hutool-cron/src/main/java/cn/hutool/cron/timingwheel/TimingWheel.java index a67ff667d..39f0f9bdd 100644 --- a/hutool-cron/src/main/java/cn/hutool/cron/timingwheel/TimingWheel.java +++ b/hutool-cron/src/main/java/cn/hutool/cron/timingwheel/TimingWheel.java @@ -72,6 +72,7 @@ public class TimingWheel { this.wheelSize = wheelSize; this.interval = tickMs * wheelSize; this.timerTaskLists = new TimerTaskList[wheelSize]; + initTimerTaskList(); //currentTime为tickMs的整数倍 这里做取整操作 this.currentTime = currentTime - (currentTime % tickMs); this.consumer = consumer; @@ -93,12 +94,7 @@ public class TimingWheel { long virtualId = expiration / tickMs; int index = (int) (virtualId % wheelSize); StaticLog.debug("tickMs: {} ------index: {} ------expiration: {}", tickMs, index, expiration); - TimerTaskList timerTaskList = timerTaskLists[index]; - if (null == timerTaskList) { - timerTaskList = new TimerTaskList(); - timerTaskLists[index] = timerTaskList; - } timerTaskList.addTask(timerTask); if (timerTaskList.setExpiration(virtualId * tickMs)) { //添加到delayQueue中 @@ -140,4 +136,13 @@ public class TimingWheel { } return overflowWheel; } + + /** + * 初始 timerTaskLists + */ + private void initTimerTaskList() { + for (int i = 0; i < this.timerTaskLists.length; i++) { + this.timerTaskLists[i] = new TimerTaskList(); + } + } }