当前位置: 代码网 > it编程>编程语言>Java > SpringBoot使用@Scheduled实现定时任务的并行执行

SpringBoot使用@Scheduled实现定时任务的并行执行

2024年06月12日 Java 我要评论
引言在springboot中,如果使用@scheduled注解来定义多个定时任务,默认情况下这些任务将会被安排在一个单线程的调度器中执行。这意味着,这些任务将会串行执行,而不是并行执行。当一个任务正在

引言

在springboot中,如果使用@scheduled注解来定义多个定时任务,默认情况下这些任务将会被安排在一个单线程的调度器中执行。这意味着,这些任务将会串行执行,而不是并行执行。当一个任务正在执行时,其他被触发的任务将会等待当前任务完成后再开始执行,这可能导致任务执行上的阻塞,特别是当某个任务执行时间较长时,可能会延迟后续任务的启动时间,影响定时任务的准时性。

1.问题代码及测试结果

问题代码:

    @scheduled(cron = "*/1 * * * * *")
    public void a() throws interruptedexception {
        log.info("a start {}!", system.currenttimemillis());
        thread.sleep(2000);
        log.info("a end {}!", system.currenttimemillis());
    }

    @scheduled(cron = "*/1 * * * * *")
    public void b() {
        log.info("b start {}!", system.currenttimemillis());
        log.info("b end {}!", system.currenttimemillis());
    }

部分测试结果:

15:38:29.001 [scheduling-1] info  c.x.e.m.schedulertask - [b,52] - b start 1716968309001!
15:38:29.001 [scheduling-1] info  c.x.e.m.schedulertask - [b,53] - b end 1716968309001!
15:38:29.001 [scheduling-1] info  c.x.e.m.schedulertask - [a,44] - a start 1716968309001!

15:38:31.003 [scheduling-1] info  c.x.e.m.schedulertask - [a,46] - a end 1716968311003!
15:38:31.003 [scheduling-1] info  c.x.e.m.schedulertask - [b,52] - b start 1716968311003!
15:38:31.003 [scheduling-1] info  c.x.e.m.schedulertask - [b,53] - b end 1716968311003!

15:38:32.002 [scheduling-1] info  c.x.e.m.schedulertask - [a,44] - a start 1716968312002!

15:38:34.003 [scheduling-1] info  c.x.e.m.schedulertask - [a,46] - a end 1716968314003!
15:38:34.003 [scheduling-1] info  c.x.e.m.schedulertask - [b,52] - b start 1716968314003!
15:38:34.003 [scheduling-1] info  c.x.e.m.schedulertask - [b,53] - b end 1716968314003!

结果分析:

  • a和b是串行的。

2.定时任务实现并行

2.1 使用自定义线程池(添加类)

可以通过配置一个自定义的taskscheduler或者threadpooltaskscheduler来为@scheduled任务提供一个线程池,从而允许多个任务并行执行。例如,可以在配置类中定义一个threadpooltaskscheduler bean:

@configuration
public class asyncconfig {
    @bean
    public threadpooltaskscheduler taskscheduler() {
        threadpooltaskscheduler scheduler = new threadpooltaskscheduler();
        // 设置线程池大小
        scheduler.setpoolsize(10); 
        scheduler.setthreadnameprefix("my-scheduled-task-");
        return scheduler;
    }
}

并确保你的配置类被扫描到,且在@enablescheduling注解的上下文中。

测试代码:

    @scheduled(cron = "*/1 * * * * *")
    public void a() throws interruptedexception {
        log.info("a start {}!", system.currenttimemillis());
        thread.sleep(2000);
        log.info("a end {}!", system.currenttimemillis());
    }

    @scheduled(cron = "*/1 * * * * *")
    public void b() {
        log.info("b start {}!", system.currenttimemillis());
        log.info("b end {}!", system.currenttimemillis());
    }

部分测试结果:

15:16:18.003 [my-scheduled-task-2] info  c.x.e.m.schedulertask - [a,44] - a start 1716966978003!
15:16:18.003 [my-scheduled-task-1] info  c.x.e.m.schedulertask - [b,52] - b start 1716966978003!
15:16:18.003 [my-scheduled-task-1] info  c.x.e.m.schedulertask - [b,53] - b end 1716966978003!

15:16:19.002 [my-scheduled-task-1] info  c.x.e.m.schedulertask - [b,52] - b start 1716966979002!
15:16:19.002 [my-scheduled-task-1] info  c.x.e.m.schedulertask - [b,53] - b end 1716966979002!

15:16:20.004 [my-scheduled-task-2] info  c.x.e.m.schedulertask - [a,46] - a end 1716966980004!
15:16:20.004 [my-scheduled-task-3] info  c.x.e.m.schedulertask - [b,52] - b start 1716966980004!
15:16:20.004 [my-scheduled-task-3] info  c.x.e.m.schedulertask - [b,53] - b end 1716966980004!

15:16:21.003 [my-scheduled-task-1] info  c.x.e.m.schedulertask - [a,44] - a start 1716966981003!
15:16:21.003 [my-scheduled-task-4] info  c.x.e.m.schedulertask - [b,52] - b start 1716966981003!
15:16:21.003 [my-scheduled-task-4] info  c.x.e.m.schedulertask - [b,53] - b end 1716966981003!

15:16:22.001 [my-scheduled-task-2] info  c.x.e.m.schedulertask - [b,52] - b start 1716966982001!
15:16:22.001 [my-scheduled-task-2] info  c.x.e.m.schedulertask - [b,53] - b end 1716966982001!

15:16:23.004 [my-scheduled-task-1] info  c.x.e.m.schedulertask - [a,46] - a end 1716966983004!
15:16:23.004 [my-scheduled-task-3] info  c.x.e.m.schedulertask - [b,52] - b start 1716966983004!
15:16:23.004 [my-scheduled-task-3] info  c.x.e.m.schedulertask - [b,53] - b end 1716966983004!

结果分析:

  • a和b是并行的;
  • a和a或者b和b是串行的。

2.2 使用异步处理(添加类和注解)

结合@async注解和@enableasync可以使得每个@scheduled任务在独立的线程中异步执行。
首先需要在配置类中启用异步支持,并配置一个线程池,然后在每个定时任务方法上添加@async注解。

@enableasync
@configuration
public class asyncconfig {
    @bean
    public executor taskexecutor() {
        threadpooltaskexecutor executor = new threadpooltaskexecutor();
        executor.setcorepoolsize(10);
        executor.setmaxpoolsize(20);
        executor.setqueuecapacity(200);
        executor.setthreadnameprefix("async-");
        executor.initialize();
        return executor;
    }
}

测试代码:

    @async
    @scheduled(cron = "*/1 * * * * *")
    public void a() throws interruptedexception {
        log.info("a start {}!", system.currenttimemillis());
        thread.sleep(2000);
        log.info("a end {}!", system.currenttimemillis());
    }

    @async
    @scheduled(cron = "*/1 * * * * *")
    public void b() {
        log.info("b start {}!", system.currenttimemillis());
        log.info("b end {}!", system.currenttimemillis());
    }

部分测试结果:

15:26:52.008 [async-2] info  c.x.e.m.schedulertask - [a,44] - a start 1716967612008!
15:26:52.008 [async-1] info  c.x.e.m.schedulertask - [b,52] - b start 1716967612008!
15:26:52.009 [async-1] info  c.x.e.m.schedulertask - [b,53] - b end 1716967612009!

15:26:53.002 [async-4] info  c.x.e.m.schedulertask - [a,44] - a start 1716967613002!
15:26:53.002 [async-3] info  c.x.e.m.schedulertask - [b,52] - b start 1716967613002!
15:26:53.002 [async-3] info  c.x.e.m.schedulertask - [b,53] - b end 1716967613002!

15:26:54.001 [async-6] info  c.x.e.m.schedulertask - [a,44] - a start 1716967614001!
15:26:54.001 [async-5] info  c.x.e.m.schedulertask - [b,52] - b start 1716967614001!
15:26:54.001 [async-5] info  c.x.e.m.schedulertask - [b,53] - b end 1716967614001!
15:26:54.010 [async-2] info  c.x.e.m.schedulertask - [a,46] - a end 1716967614010!

15:26:55.002 [async-8] info  c.x.e.m.schedulertask - [a,44] - a start 1716967615002!
15:26:55.002 [async-7] info  c.x.e.m.schedulertask - [b,52] - b start 1716967615002!
15:26:55.002 [async-7] info  c.x.e.m.schedulertask - [b,53] - b end 1716967615002!
15:26:55.002 [async-4] info  c.x.e.m.schedulertask - [a,46] - a end 1716967615002!

15:26:56.001 [async-10] info  c.x.e.m.schedulertask - [a,44] - a start 1716967616001!
15:26:56.001 [async-9] info  c.x.e.m.schedulertask - [b,52] - b start 1716967616001!
15:26:56.001 [async-9] info  c.x.e.m.schedulertask - [b,53] - b end 1716967616001!
15:26:56.002 [async-6] info  c.x.e.m.schedulertask - [a,46] - a end 1716967616002!

15:26:57.001 [async-3] info  c.x.e.m.schedulertask - [a,44] - a start 1716967617001!
15:26:57.001 [async-1] info  c.x.e.m.schedulertask - [b,52] - b start 1716967617001!
15:26:57.001 [async-1] info  c.x.e.m.schedulertask - [b,53] - b end 1716967617001!
15:26:57.002 [async-8] info  c.x.e.m.schedulertask - [a,46] - a end 1716967617002!

测试结果分析:

  • a和b是并行的;
  • a和a或者b和b也是并行的。

3.总结

到此这篇关于springboot使用@scheduled实现定时任务的并行执行的文章就介绍到这了,更多相关springboot @scheduled任务并行执行内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com