spring boot自带定时器,
默认是单线程执行,多个定时任务同一时刻,只会执行一个(串行)
可配置线程池,多线程执行(并行)
单线程版
@Component
public class sud {
@Scheduled(cron = "0 0/1 * * * ?")
public void test01() {
System.out
.println(Thread.currentThread().getId() + "------每分钟执行一次-------01" + Thread.currentThread().getName());
}
@Scheduled(cron = "*/5 * * * * ?")
public void test04() {
System.out.println(
Thread.currentThread().getId() + "=========每5秒执行一次========" + Thread.currentThread().getName());
throw new RuntimeException();
}
@Scheduled(cron = "*/1 * * * * ?")
public void test05() {
System.out.println(
Thread.currentThread().getId() + "==!!!=======每1秒执行一次====!!!====" + Thread.currentThread().getName());
}
}
启动类,记得加此注解
多线程版(只需配置线程池即可),启动类上就不需要加开启定时器的注解
这种定时器有很大弊端,在公司中一般使用MQ,或者可执行jar的方式