关于@Scheduled注解中cron表达式使用*/x与0/x的区别
关于@Scheduled注解中cron表达式使用*/x与0/x的区别:
1.在@Scheduled注解中使用*/x,代表在系统启动时,每过x时间执行一次对应的方法。
例如:
@Scheduled(cron = "0 */10 * * * ?")
public void printHello(){
System.out.println("Hello");
}
上面例子代表系统启动时每过10分钟,就要执行一遍printHello()方法。如果系统启动时时间是2023-10-01 14:05:00,那么下一次执行时间便是 2023-10-01 14:15:00
2.而0/x代表从0开始每过x段时间对应的方法就执行一次。
例如:
@Scheduled(cron = "0 0/10 * * * ? ")
public void printHello(){
System.out.println("Hello");
}
上面例子代表着系统启动时,以0开始,每过10分钟执行一次printHello()方法。如果系统启动时时间是2023-10-01 14:05:00,那么下一次执行时间便是2023-10-01 14:10:00。