当前位置:   article > 正文

定时任务@Scheduled参数详解_@scheduled(fixeddelay = 2000)

@scheduled(fixeddelay = 2000)

定时任务@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();
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

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]
  • 1
  • 2
  • 3
  • 4
  • 5

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]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
@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]

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/728197
推荐阅读
相关标签
  

闽ICP备14008679号