1.首先是springboot
类上@EnableAsync 开启异步任务
类的具体方法上加上@Async

两个都加了才有作用,不然如果一个任务没执行完,后面的任务就会卡住

eg:


@Component
@EnableScheduling
@EnableAsync
public class TaskTest1 {

    @Async
    @Scheduled(cron = "0/10 * * * * ?")
    public void sendA(){
        System.out.println("A");
        try {
            System.err.println("A 阻塞 90000millis");
            Thread.sleep(90000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Async
    @Scheduled(cron = "0/10 * * * * ?")
    public void sendB(){
        System.out.println("B");
        try {
            System.err.println("B 阻塞 90000millis");
            Thread.sleep(90000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Async
    @Scheduled(cron = "0/5 * * * * ?")
    //或直接指定时间间隔,例如:5秒
    //@Scheduled(fixedRate=5000)
    public void configureTasks() {
        System.err.println("执行静态定时任务时间: " + LocalDateTime.now());
    }
}

image.png