定時任務@Scheduled引數詳解

點選抽獎發表於2020-10-23

定時任務@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 : 初始化延遲執行時間.

@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]

相關文章