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,如需转载请自行联系原作者



相关文章
|
1月前
|
druid Java 数据库
Spring Boot的定时任务与异步任务
Spring Boot的定时任务与异步任务
|
5月前
|
Java
springboot使用异步任务
springboot使用异步任务
60 0
|
22天前
|
Java Spring
SpringBoot+async异步调用接口以及几个任务同时完成和异步接口实现和调用
SpringBoot+async异步调用接口以及几个任务同时完成和异步接口实现和调用
22 0
|
1月前
|
NoSQL Java 数据库
【三十】springboot项目上高并发解决示例
【三十】springboot项目上高并发解决示例
48 1
|
6月前
|
Java Unix 调度
springboot快速整合任务
springboot快速整合任务
|
3月前
|
SQL 分布式计算 Java
SpringBoot集成quartz调度linkis任务
SpringBoot集成quartz调度linkis任务
|
3月前
|
Java 调度 流计算
在使用Spring Boot启动Flink处理任务时
在使用Spring Boot启动Flink处理任务时【1月更文挑战第22天】【1月更文挑战第108篇】
54 1
|
3月前
|
Java 容器
SpringBoot 异步任务处理
SpringBoot 异步任务处理
16 0
|
4月前
|
Java
Springboot整合Activity7:任务,历史任务,UEL表达式(三)
Springboot整合Activity7:任务,历史任务,UEL表达式(三)
|
4月前
|
Java
Springboot整合Activity7:任务,历史任务,UEL表达式(二)
Springboot整合Activity7:任务,历史任务,UEL表达式(二)