首页 > 编程语言 > 详细

SpringBoot中定时任务默认是串行执行 如何设置并行

时间:2019-04-11 12:48:29      阅读:871      评论:0      收藏:0      [点我收藏+]

SpringBoot项目中,默认是串行执行的,不论启动多少任务,都是一个执行完成,再执行下一个。

 

如何设置并行呢? @EnableAsync  和@Async 这两个注解来实现 ,具体如下:

 

@EnableScheduling
@EnableAsync
@SpringBootApplication
public class App
{
    public static void main( String[] args )
    {
        System.out.println( "Hello World!" );

        SpringApplication app = new SpringApplication(App.class);
        //关闭banner
        app.setBannerMode(Banner.Mode.OFF);
        app.run(args);
    }
}
@Service
@Async
public class TaskSecond {

    private Logger logger = LoggerFactory.getLogger(TaskSecond.class);
    //@Scheduled( cron = "0/2 * * * * * ")
    @Scheduled(fixedDelay = 20000,initialDelay = 2000)
    public void test(){
        try {
            logger.info(" second start");

            TimeUnit.SECONDS.sleep(10);
           logger.info(" second end");

        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
@Service
@Async
public class TaskFirst {
    private Logger logger = LoggerFactory.getLogger(TaskSecond.class);
    //@Scheduled( cron = "0/2 * * * * * ")
    @Scheduled(fixedDelay = 20000,initialDelay = 2000)
    public void test(){
        try {
            logger.info(" first start");

            TimeUnit.SECONDS.sleep(10);
            logger.info(" first end");

        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

 

SpringBoot中定时任务默认是串行执行 如何设置并行

原文:https://www.cnblogs.com/liuxm2017/p/10688814.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!