1.多个任务单线程执行(默认)
@Component
@EnableScheduling // 开启定时任务,(这个注解用在启动类上的话,就不用每个task类都写一句@EnableScheduling
public class Task {
@Scheduled(cron = "0/60 * * * * ?")//60s执行一次
private void task1(){
System.out.println("定时任务一,恭喜发财~" + LocalDateTime.now().toLocalTime() + "," + Thread.currentThread().getName());
}
@Scheduled(cron = "0/60 * * * * ?")//60s执行一次
private void task2(){
System.out.println("定时任务二,恭喜发财~" + LocalDateTime.now().toLocalTime() + "," + Thread.currentThread().getName());
}
}
2.多线程异步执行(注解@Async)
@Component
@EnableScheduling
@EnableAsync // 开启多线程
public class Task {
@Async
@Scheduled(cron = "0/30 * * * * ?")