定时任务@Scheduled
任务间隔2秒,处理耗时5秒.
@Async
@Scheduled(initialDelay = 5000,fixedRate = 2000)
// @Scheduled(initialDelay = 5000,fixedDelay = 2000)
public void test() {
LogUtils.info("定时任务开始执行:[{}]", sf.format(new Date()));
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
initialDelay
= 5000 : 初始化延迟执行时间.
@Async 关闭
fixedDelay
= 2000 ,任务耗时+定时周期时间 = 下次执行时间
2020-10-23 18:11:24.814 [scheduling-1] INFO com.wmang.schedule.basic.util.LogUtils - 定时任务开始执行:[2020-10-23 18:11:24]
2020-10-23 18:11:31.815 [scheduling-1] INFO com.midea.mgp.carrier.basic.util.LogUtils - 定时任务开始执行:[2020-10-23 18:11:31]
2020-10-23 18:11:38.816 [scheduling-1] INFO com.midea.mgp.carrier.basic.util.LogUtils - 定时任务开始执行:[2020-10-23 18:11:38]
2020-10-23 18:11:45.816 [scheduling-1] INFO com.midea.mgp.carrier.basic.util.LogUtils - 定时任务开始执行:[2020-10-23 18:11:45]
2020-10-23 18:11:52.817 [scheduling-1] INFO com.midea.mgp.carrier.basic.util.LogUtils - 定时任务开始执行:[2020-10-23 18:11:52]
fixedRate
= 2000 任务执行完才会开始下次任务执行.周期计算不加任务耗时
2020-10-23 18:13:34.745 [scheduling-1] INFO com.midea.mgp.carrier.basic.util.LogUtils - 定时任务开始执行:[2020-10-23 18:13:34]
2020-10-23 18:13:39.745 [scheduling-1] INFO com.midea.mgp.carrier.basic.util.LogUtils - 定时任务开始执行:[2020-10-23 18:13:39]
2020-10-23 18:13:44.745 [scheduling-1] INFO com.midea.mgp.carrier.basic.util.LogUtils - 定时任务开始执行:[2020-10-23 18:13:44]
2020-10-23 18:13:49.746 [scheduling-1] INFO com.midea.mgp.carrier.basic.util.LogUtils - 定时任务开始执行:[2020-10-23 18:13:49]
2020-10-23 18:13:54.746 [scheduling-1] INFO com.midea.mgp.carrier.basic.util.LogUtils - 定时任务开始执行:[2020-10-23 18:13:54]
2020-10-23 18:13:59.746 [scheduling-1] INFO com.midea.mgp.carrier.basic.util.LogUtils - 定时任务开始执行:[2020-10-23 18:13:59]
@Async 开启
fixedDelay
= 2000 或者 fixedRate = 2000 效果是一样的.不会受到任务执行耗时影响
2020-10-23 18:10:10.959 [SimpleAsyncTaskExecutor-1] INFO com.midea.mgp.carrier.basic.util.LogUtils - 定时任务开始执行:[2020-10-23 18:10:10]
2020-10-23 18:10:12.954 [SimpleAsyncTaskExecutor-2] INFO com.midea.mgp.carrier.basic.util.LogUtils - 定时任务开始执行:[2020-10-23 18:10:12]
2020-10-23 18:10:14.955 [SimpleAsyncTaskExecutor-3] INFO com.midea.mgp.carrier.basic.util.LogUtils - 定时任务开始执行:[2020-10-23 18:10:14]
2020-10-23 18:10:16.954 [SimpleAsyncTaskExecutor-4] INFO com.midea.mgp.carrier.basic.util.LogUtils - 定时任务开始执行:[2020-10-23 18:10:16]
2020-10-23 18:10:18.954 [SimpleAsyncTaskExecutor-5] INFO com.midea.mgp.carrier.basic.util.LogUtils - 定时任务开始执行:[2020-10-23 18:10:18]