欢迎访问『www.cnblogs.com/blog-ice』
定时任务丢失问题时由于任务调度默认时一个线程执行scheduled-1

1.到点就执行,不管上次有没有执行完成
  @EnableAsync

    @Bean("threadPool")
    public Executor threadPool() {
        return Executors.newScheduledThreadPool(5);
    }

    @Scheduled(cron = "0/1 * * * * *")
    @Async("threadPool") // 到点就执行,不管上次有没有执行
    public void jobTwo() throws InterruptedException {
        System.out.println(String.format("%-5s %-6s %-23s %s", "2", "start", LocalDateTime.now(), Thread.currentThread().getName()));
        Thread.sleep(1500);
        System.out.println(String.format("%-5s %-6s %-23s %s", "2", "end", LocalDateTime.now(), Thread.currentThread().getName()));
    }


2.如果当前有任务正在执行,则当期任务不执行
public class ScheduledJob implements SchedulingConfigurer {

    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        taskRegistrar.setScheduler(threadPool()); // 如果当前有任务正在执行,则当期任务不执行
    }

 

posted on 2022-03-03 15:10  仙路尽头谁为峰  阅读(486)  评论(0编辑  收藏  举报
这里是自由发挥的天堂