开发者

Scheduled如何会在上次任务执行完才会执行下次任务

开发者 https://www.devze.com 2024-08-10 13:51 出处:网络 作者: hashdog
目录Scheduled会在上次任务执行完才会执行下次任务在使用spring的Scheduled定时任务注释掉synchronized总结Scheduled会在上次任务执行完才会执行下次任务
目录
  • Scheduled会在上次任务执行完才会执行下次任务
    • 在使用spring的Scheduled定时任务
    • 注释掉synchronized
  • 总结

    Scheduled会在上次任务执行完才会执行下次任务

    在使用spring的Scheduled定时任务

    担心同一任务,

    第一次开始未执行完就执行第二次任务,所以给加了synchronized,

    但是后面经过测试Scheduled定时任务会在上次任务结束时再执行第二次任务,

    如果第二次任务堵在哪里了,时间会顺延

    package xyz.hashdog.job;
    
    import lombok.AllArgsConstructor;
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.schedulhttp://www.devze.coming.annotation.Scheduled;
    import org.springframework.stereotype.Component;
    
    /**
     * @author th
     * @description: 定时任务测试
     * @projectName hashdog-ds
     * @date 2020/2/1223:07
     */
    @Component
    @Slf4j
    @AllArgsConstructor
    public class TestCrontab {
        private static final Object KEY = new Object();
    
        private static boolean taskFlag = false;
    
        @Scheduled(cron = "*/5 * * * * ?")
        public void pushCancel() {
            System.out.println("进来了");
            synchronized (KEY) {
                if (TestCrontab.taskFlag) {
                    System.out.println("测试调度已经启动");
                    log.warn("测试调度已经启动");
                    return;
                }
                TestCrontab.taskFlag = true;
            }
    
            try {
                for (int i =0;i<=10;i++){
                    System.out.println("执行:"+i);
                    Thread.sleep(2000);
        android        }yjeRSXKG
            } catch (Exception e) {
                log.error("测试调度执行出错", e);
            }
    
            TestCrontab.taskFlag = false;
    
            log.warn("测试调度执行完成");
        }
    
    
    }

    注释掉synchronized

    执行效果一样,并没有线程安全问题

    package xyz.hashdog.job;
    
    import lombok.AllArgsConstructor;
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.scheduling.annotation.Scheduled;
    import org.springframework.stereotype.Com编程客栈ponent;
    
    /**
     * @author th
     * @description: 定时任务测试
     * @projectName hashdog-ds
     * @date 2020/2/1223:07
     */
    @Component
    @Slf4j
    @AllArgsConstructor
    public class TestCrontab {
        private static final Object KEY = new Object();
    
        private static boolean taskFlag = false;
    
        @Scheduled(cron = "*/5 * * * * ?")
        public void pushCancel() throws InterruptedException {
            System.out.println("进来了");
    //        synchronized (KEY) {
    //            if (TestCrontab.taskFlag) {
    //                System.out.println("测试调度已经启动");
    //                log.w编程客栈arn("测试调度已经启动");
    //                return;
    //            }
    //            TestCrontab.taskFlag = true;
    //        }
    //
    //        try {
                for (int i =0;i<=10;i++){
                    System.out.println("执行:"+i);
                    Thread.sleep(2000);
                }
    //        } catch (Exception e) {
    //            log.error("测试调度执行出错", e);
    //        }
    //
    //        TestCrontab.taskFlag = false;
    
            log.warn("测试调度执行完成");
        }
    
    
    }

    总结

    以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程客栈(www.devze.com)。

    0

    精彩评论

    暂无评论...
    验证码 换一张
    取 消

    关注公众号