springboot + @scheduled 多任务并发

简介:

一、问题


项目采用springboot搭建,想给方法添加@Scheduled注解,实现两个定时任务。可是运行发现,两个task并没有并发执行,而是执行完一个task才会执行另外一个。上代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package  com.autohome.contentplatform.tasks;
 
import  org.springframework.beans.factory.annotation.Configurable;
import  org.springframework.scheduling.annotation.EnableScheduling;
import  org.springframework.scheduling.annotation.Scheduled;
import  org.springframework.stereotype.Component;
 
@Component
@Configurable
@EnableScheduling
public  class  task1 {
      @Scheduled (cron =  "0/5 * *  * * ? " )
      public  void  startSchedule() {
          System.out.println( "===========1=>" );
          try  {
              for ( int  i= 1 ;i<= 10 ;i++){
                  System.out.println( "=1==>" +i);
                  Thread.sleep( 1000 );
              }
          catch  (InterruptedException e) {
              e.printStackTrace();
          }
      }
      @Scheduled (cron =  "0/5 * *  * * ? " )
      public  void  startSchedule2() {
          for ( int  i= 1 ;i<= 10 ;i++){
              System.out.println( "=2==>" +i);
              try  {
                  Thread.sleep( 1000 );
              catch  (InterruptedException e) {
                  e.printStackTrace();
              }
          }
      }
}

 

运行发现任务没有并行执行。

二、解决


给类添加注解@EnableAsync,并给方法添加注解@Async。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
@Component
@Configurable
@EnableScheduling
@EnableAsync
public  class  DemoTask {
      @Async
      @Scheduled (cron =  "0/5 * *  * * ? " )
      public  void  startSchedule() {
          System.out.println( "===========1=>" );
          try  {
              for ( int  i= 1 ;i<= 10 ;i++){
                  System.out.println( "=1==>" +i);
                  Thread.sleep( 1000 );
              }
          catch  (InterruptedException e) {
              e.printStackTrace();
          }
      }
 
     @Async
      @Scheduled (cron =  "0/5 * *  * * ? " )
      public  void  startSchedule2() {
          for ( int  i= 1 ;i<= 10 ;i++){
              System.out.println( "=2==>" +i);
              try  {
                  Thread.sleep( 1000 );
              catch  (InterruptedException e) {
                  e.printStackTrace();
              }
          }
      }
}

 

再次运行,发现两个任务可以并发执行了。

三、参考资料:

https://docs.spring.io/spring/docs/3.2.x/spring-framework-reference/html/scheduling.html

 






    本文转自 陈敬(Cathy) 博客园博客,原文链接:http://www.cnblogs.com/janes/p/8085654.html,如需转载请自行联系原作者



相关文章
|
人工智能
使用CodeBuddy实现网页自动连点器
CodeBuddy 能够迅速理解复杂功能要求,精准生成自动连点器代码。无论是游戏场景里对技能释放点击频率的精确控制,还是办公场景中对特定单元格点击位置的灵活设定,它都能高效满足。
544 6
SpringBoot打包时将lib放外面
SpringBoot打包时将lib放外面
724 0
|
JSON fastjson 数据格式
震惊!fastjson SerializerFeature详解竟然是这个样子
震惊!fastjson SerializerFeature详解竟然是这个样子
628 0
震惊!fastjson SerializerFeature详解竟然是这个样子
|
算法 计算机视觉
【数据结构入门精讲 | 第十六篇】并查集知识点及考研408、企业面试练习
【数据结构入门精讲 | 第十六篇】并查集知识点及考研408、企业面试练习
506 0
|
Java BI 调度
在Spring Boot中实现多线程任务调度
在Spring Boot中实现多线程任务调度
|
NoSQL Java 调度
在Spring Boot中实现分布式任务调度
在Spring Boot中实现分布式任务调度
永磁同步电机自抗扰伺服控制算法仿真模型研究#毕业设计
永磁同步电机自抗扰伺服控制算法仿真模型研究#毕业设计
|
XML API 开发者
产品评论获取API接口
在电商与数据分析中,产品评论是重要用户反馈。本文介绍如何通过API接口自动化高效获取评论,涵盖核心概念、实现步骤及Python示例,助您快速构建数据采集流程,提升分析效率与决策能力。
|
缓存 Java 数据库连接
Mybatis缓存相关面试题有多卷
使用 MyBatis 缓存机制需要注意以下几点: 对于频繁更新和变动的数据,不适合使用缓存。 对于数据的一致性要求比较高的场景,不适合使用缓存。 如果配置了二级缓存,需要确保缓存的数据不会影响到其他业务模块的数据。 在使用缓存时,需要注意缓存的命中率和缓存的过期策略,避免缓存过期导致查询性能下降。
535 0

热门文章

最新文章