mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-12-06 17:18:54 +08:00
修复CronTimer在任务非常多时,追赶系统时间导致遗漏任务的问题(issue#IB49EF@Gitee)
This commit is contained in:
parent
a4d1c963f6
commit
ec5c10e233
@ -55,32 +55,38 @@ public class CronTimer extends Thread implements Serializable {
|
||||
@Override
|
||||
public void run() {
|
||||
final long timerUnit = this.scheduler.config.matchSecond ? TIMER_UNIT_SECOND : TIMER_UNIT_MINUTE;
|
||||
final long doubleTimeUnit = 2 * timerUnit;
|
||||
|
||||
long thisTime = System.currentTimeMillis();
|
||||
long nextTime;
|
||||
long sleep;
|
||||
while(!isStop){
|
||||
spawnLauncher(thisTime);
|
||||
|
||||
//下一时间计算是按照上一个执行点开始时间计算的
|
||||
//此处除以定时单位是为了清零单位以下部分,例如单位是分则秒和毫秒清零
|
||||
long nextTime = ((thisTime / timerUnit) + 1) * timerUnit;
|
||||
final long sleep = nextTime - System.currentTimeMillis();
|
||||
if(sleep < 0){
|
||||
// 可能循环执行慢导致时间点跟不上系统时间,追赶系统时间并执行中间差异的时间点(issue#IB49EF@Gitee)
|
||||
thisTime = System.currentTimeMillis();
|
||||
while(nextTime <= thisTime){
|
||||
// 追赶系统时间并运行执行点
|
||||
spawnLauncher(nextTime);
|
||||
nextTime = ((thisTime / timerUnit) + 1) * timerUnit;
|
||||
sleep = nextTime - System.currentTimeMillis();
|
||||
if(isValidSleepMillis(sleep, timerUnit)){
|
||||
if (!ThreadUtil.safeSleep(sleep)) {
|
||||
//等待直到下一个时间点,如果被中断直接退出Timer
|
||||
}
|
||||
continue;
|
||||
} else if(sleep > doubleTimeUnit){
|
||||
// 时间回退,可能用户回拨了时间或自动校准了时间,重新计算(issue#1224@Github)
|
||||
thisTime = System.currentTimeMillis();
|
||||
continue;
|
||||
} else if (!ThreadUtil.safeSleep(sleep)) {
|
||||
//等待直到下一个时间点,如果被用户中断直接退出Timer
|
||||
break;
|
||||
}
|
||||
|
||||
//执行点,时间记录为执行开始的时间,而非结束时间
|
||||
spawnLauncher(nextTime);
|
||||
|
||||
// issue#3460 采用叠加方式,确保正好是1分钟或1秒,避免sleep晚醒问题
|
||||
// 此处无需校验,因为每次循环都是sleep与上触发点的时间差。
|
||||
// 当上一次晚醒后,本次会减少sleep时间,保证误差在一个unit内,并不断修正。
|
||||
thisTime = nextTime;
|
||||
} else{
|
||||
// 非正常时间重新计算(issue#1224@Github)
|
||||
thisTime = System.currentTimeMillis();
|
||||
}
|
||||
}
|
||||
log.debug("Hutool-cron timer stopped.");
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user