springboot中@EnableAsync与@Async注解使用

@Async为异步注解,放到方法上,表示调用该方法的线程与此方法异步执行,需要配合@EnableAsync注解使用。

1、首先演示没有@Async,即没有异步执行的情况

      -  创建一个普通类CountNumber,并注入到IOC容器中



springboot中@EnableAsync与@Async注解使用_spring

1 package com.example.demo;
2 import org.springframework.scheduling.annotation.Async;
3 import org.springframework.stereotype.Component;
4
5 @Component
6 public class CountNumber {
7
8 public void PrintNumber(){
9 for(int i=1; i<10; i++){
10 System.out.println("i = " + i);
11 }
12 }
13 }


springboot中@EnableAsync与@Async注解使用_spring


     - 在spring boot的启动类中获取IOC的bean



springboot中@EnableAsync与@Async注解使用_spring

1 package com.example.demo;
2 import java.util.concurrent.TimeUnit;
3 import org.springframework.boot.SpringApplication;
4 import org.springframework.boot.autoconfigure.SpringBootApplication;
5 import org.springframework.context.ConfigurableApplicationContext;
6 import org.springframework.context.annotation.ComponentScan;
7
8 //@SpringBootApplication
9 @ComponentScan
10 public class Springboot3Application {
11
12 public static void main(String[] args) throws Exception {
13
14 ConfigurableApplicationContext context = SpringApplication.run(Springboot3Application.class, args);
15
16 context.getBean(CountNumber.class).PrintNumber();
17 for(int i=1; i<10; i++){
18 System.out.println("------------------");
19 }
20 context.close();
21 }
22 }


springboot中@EnableAsync与@Async注解使用_spring


    - 运行输出结果:



springboot中@EnableAsync与@Async注解使用_spring

i = 1
i = 2
i = 3
i = 4
i = 5
i = 6
i = 7
i = 8
i = 9
------------------
------------------
------------------
------------------
------------------
------------------
------------------
------------------
------------------


springboot中@EnableAsync与@Async注解使用_spring


  从输出结果中可以看出,启动类先从IOC容器中获取CountNumber的对象,然后执行该对象的PrintNumber方法,循环打印了9个数字,方法执行结束后,继续回到启动类中往下执行,因此开始执行for循环语句。从整个流程看属于顺序执行的。

 

 

 

2、有@Async,即异步执行的情况

    - 创建一个普通类CountNumber,并注入到IOC容器中,并在该类的方法上标注@Async注解,表示该方法是异步执行的。



springboot中@EnableAsync与@Async注解使用_spring

1 package com.example.demo;
2 import org.springframework.scheduling.annotation.Async;
3 import org.springframework.stereotype.Component;
4
5 @Component
6 public class CountNumber {
7 @Async
8 public void PrintNumber(){
9 for(int i=1; i<10; i++){
10 System.out.println("i = " + i);
11 }
12 }
13 }


springboot中@EnableAsync与@Async注解使用_spring


     -  从spring boot启动类中获取IOC中的bean,在启动类上标注@EnableAsync注解,启动@Async异步注解。或者在启动类中只标注@SpringBootApplication注解,因为该注解中已经包含了上面两个注解。



springboot中@EnableAsync与@Async注解使用_spring

1 package com.example.demo;
2 import java.util.concurrent.TimeUnit;
3 import org.springframework.boot.SpringApplication;
4 import org.springframework.boot.autoconfigure.SpringBootApplication;
5 import org.springframework.context.ConfigurableApplicationContext;
6 import org.springframework.context.annotation.ComponentScan;
7 import org.springframework.scheduling.annotation.Async;
8 import org.springframework.scheduling.annotation.EnableAsync;
9
10 /*@SpringBootApplication注解与@ComponentScan、@EnableAsync注解达到相同的功效*/
11 //@SpringBootApplication
12 @ComponentScan
13 @EnableAsync
14 public class Springboot3Application {
15
16 public static void main(String[] args) throws Exception {
17
18 ConfigurableApplicationContext context = SpringApplication.run(Springboot3Application.class, args);
19
20 /*@Async和@EnableAsync配合使用*/
21 context.getBean(CountNumber.class).PrintNumber();
22 for(int i=1; i<10; i++){
23 TimeUnit.MICROSECONDS.sleep(1);
24 System.out.println("------------------");
25 }
26 context.close();
27 }
28 }


springboot中@EnableAsync与@Async注解使用_spring


    - 执行启动类,输出结果如下:



springboot中@EnableAsync与@Async注解使用_spring

------------------
------------------
------------------
------------------
------------------
------------------
i = 1
i = 2
i = 3
i = 4
i = 5
i = 6
i = 7
i = 8
i = 9
------------------
------------------
------------------


springboot中@EnableAsync与@Async注解使用_spring


     从输出结果中可以看出,spring boot在获取到IOC中的CountNumber对象后,一方面继续向下执行,执行for循环语句,另一方面获取对象后,执行对象中的PrintNumber方法。因此PrintNumber方法是与主线程是异步执行的。