@Async注解配合@EnableAsync注解使用
前言
@Async為異步注解,放到需要使用異步的方法上面,表示調用該方法的線程與此方法異步執行,需要配合@EnableAsync注解使用。
沒有異步執行,沒有@Async注解時
1、創建一個普通的類,并注入到IOC容器中
package com.example.demo;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
@Component
public class CountNumber {
? ? public void PrintNumber(){
? ? ? ? for(int i=1; i<10; i++){
? ? ? ? ? ? System.out.println("i = " + i);
? ? ? ? }
? ? }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
2、在Spring Boot的啟動類中獲取IOC的bean
package com.example.demo;
import java.util.concurrent.TimeUnit;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
//@SpringBootApplication
@ComponentScan
public class Springboot3Application {
? ? public static void main(String[] args) throws Exception {
? ? ? ? ConfigurableApplicationContext context = SpringApplication.run(Springboot3Application.class, args);
? ? context.getBean(CountNumber.class).PrintNumber();
? ? ? ? for(int i=1; i<10; i++){
? ? ? ? ? ? System.out.println("------------------");
? ? ? ? }
? ? ? ? context.close();
? ? }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
3、運行輸出結果
i = 1
i = 2
i = 3
i = 4
i = 5
i = 6
i = 7
i = 8
i = 9
------------------
------------------
------------------
------------------
------------------
------------------
------------------
------------------
------------------
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
從輸出結果中可以看出,啟動類先從IOC容器中獲取CountNumber的對象,然后執行該對象的PrintNumber方法,循環打印了9個數字,方法執行結束后,繼續回到啟動類中往下執行,因此開始執行for循環語句。從整個流程看屬于順序執行的。
異步執行任務,加入@Async注解時
1、創建一個普通類,并注入到IOC容器中,并在該類的方法上標注@Async注解,表示該方法是異步執行的
package com.example.demo;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
@Component
public class CountNumber {
? ? @Async
? ? public void PrintNumber(){
? ? ? ? for(int i=1; i<10; i++){
? ? ? ? ? ? System.out.println("i = " + i);
? ? ? ? }
? ? }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
2、從spring boot啟動類中獲取IOC中的bean,在啟動類上標注@EnableAsync注解,啟動@Async異步注解。或者在啟動類中只標注@SpringBootApplication注解,因為該注解中已經包含了上面兩個注解。
package com.example.demo;
import java.util.concurrent.TimeUnit;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
/*@SpringBootApplication注解與@ComponentScan、@EnableAsync注解達到相同的功效*/
//@SpringBootApplication
@ComponentScan
@EnableAsync
public class Springboot3Application {
? ? public static void main(String[] args) throws Exception {
? ? ? ? ConfigurableApplicationContext context = SpringApplication.run(Springboot3Application.class, args);
? ? ? ? /*@Async和@EnableAsync配合使用*/
? ? context.getBean(CountNumber.class).PrintNumber();
? ? ? ? for(int i=1; i<10; i++){
? ? ? ? ? ? TimeUnit.MICROSECONDS.sleep(1);
? ? ? ? ? ? System.out.println("------------------");
? ? ? ? }
? ? ? ? context.close();
? ? }
}
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
3、執行啟動類,輸出結果如下:
------------------
------------------
------------------
------------------
------------------
------------------
i = 1
i = 2
i = 3
i = 4
i = 5
i = 6
i = 7
i = 8
i = 9
------------------
------------------
------------------
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
從輸出結果中可以看出,spring boot在獲取到IOC中的CountNumber對象后,一方面繼續向下執行,執行for循環語句,另一方面獲取對象后,執行對象中的PrintNumber方法。因此PrintNumber方法是與主線程是異步執行的。
————————————————
版權聲明:本文為CSDN博主「時光無聲_l」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/liuziteng0228/article/details/82825320
總結
以上是生活随笔為你收集整理的@Async注解配合@EnableAsync注解使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: @RequiredArgsConstru
- 下一篇: SpringBoot中@EventLis