@Scheduled(fixedDelay = 3000)
public void task2() throws InterruptedException {
String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
log.info("task2 " + date);
Thread.sleep(6000);
}
2024-04-06 17:02:40.692 INFO 151080 --- [ scheduling-1] com.lxw.station.task.HelloTask : task2 2024-04-06 17:02:40
2024-04-06 17:02:49.693 INFO 151080 --- [ scheduling-1] com.lxw.station.task.HelloTask : task2 2024-04-06 17:02:49
2024-04-06 17:02:58.695 INFO 151080 --- [ scheduling-1] com.lxw.station.task.HelloTask : task2 2024-04-06 17:02:58
2024-04-06 17:03:07.696 INFO 151080 --- [ scheduling-1] com.lxw.station.task.HelloTask : task2 2024-04-06 17:03:07
2024-04-06 17:03:16.698 INFO 151080 --- [ scheduling-1] com.lxw.station.task.HelloTask : task2 2024-04-06 17:03:16
2024-04-06 17:03:25.701 INFO 151080 --- [ scheduling-1] com.lxw.station.task.HelloTask : task2 2024-04-06 17:03:25
2024-04-06 17:03:34.702 INFO 151080 --- [ scheduling-1] com.lxw.station.task.HelloTask : task2 2024-04-06 17:03:34
可以看出@Scheduled(fixedDelay = 3000)表示执行完第一次后延迟3000毫秒再次执行
@SneakyThrows
@Scheduled(fixedRate = 3000)
public void task4() {
String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
log.info("task4 " + date);
Thread.sleep(6000);
}
2024-04-06 17:06:14.597 INFO 35892 --- [ scheduling-1] com.lxw.station.task.HelloTask : task4 2024-04-06 17:06:14
2024-04-06 17:06:20.598 INFO 35892 --- [ scheduling-1] com.lxw.station.task.HelloTask : task4 2024-04-06 17:06:20
2024-04-06 17:06:26.599 INFO 35892 --- [ scheduling-1] com.lxw.station.task.HelloTask : task4 2024-04-06 17:06:26
2024-04-06 17:06:32.600 INFO 35892 --- [ scheduling-1] com.lxw.station.task.HelloTask : task4 2024-04-06 17:06:32
2024-04-06 17:06:38.600 INFO 35892 --- [ scheduling-1] com.lxw.station.task.HelloTask : task4 2024-04-06 17:06:38
2024-04-06 17:06:44.617 INFO 35892 --- [ scheduling-1] com.lxw.station.task.HelloTask : task4 2024-04-06 17:06:44
2024-04-06 17:06:50.617 INFO 35892 --- [ scheduling-1] com.lxw.station.task.HelloTask : task4 2024-04-06 17:06:50
2024-04-06 17:06:56.618 INFO 35892 --- [ scheduling-1] com.lxw.station.task.HelloTask : task4 2024-04-06 17:06:56
2024-04-06 17:07:02.618 INFO 35892 --- [ scheduling-1] com.lxw.station.task.HelloTask : task4 2024-04-06 17:07:02
2024-04-06 17:07:08.618 INFO 35892 --- [ scheduling-1] com.lxw.station.task.HelloTask : task4 2024-04-06 17:07:08
2024-04-06 17:07:14.619 INFO 35892 --- [ scheduling-1] com.lxw.station.task.HelloTask : task4 2024-04-06 17:07:14
2024-04-06 17:07:20.619 INFO 35892 --- [ scheduling-1] com.lxw.station.task.HelloTask : task4 2024-04-06 17:07:20
fixedRate = 3000表示第一次执行开始后3000毫秒再次执行,如果3000毫秒没有执行完成,执行完成后立即执行第二次,从日子可以看出首次执行开始时间2024-04-06 17:06:14,执行耗时6000毫秒,执行结束时间也就是2024-04-06 17:06:20,接着立即执行第二次