關(guān)于SpringBoot使用@Async的總結(jié)
SpringBoot使用@Async總結(jié)
注意事項(xiàng):
如下方式會(huì)使@Async失效
1. 異步方法使用static修飾
2. 異步類沒有使用@Component注解(或其他注解)導(dǎo)致spring無法掃描到異步類
3. 異步方法不能與異步方法在同一個(gè)類中
4. 類中需要使用@Autowired或@Resource等注解自動(dòng)注入,不能自己手動(dòng)new對(duì)象
5. 如果使用SpringBoot框架必須在啟動(dòng)類中增加@EnableAsync注解
6. 在Async 方法上標(biāo)注@Transactional是沒用的。 在Async 方法調(diào)用的方法上標(biāo)注@Transactional 有效。
SpringBoot實(shí)現(xiàn)異步(Async)接口
1. 啟動(dòng)類引入@EnableAsync注解
@SpringBootApplication
@EnableAsync
public class Application{
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
2. 建立異步任務(wù)類
我們建了3個(gè)異步任務(wù),分別延遲1s,2s,3s
@Component
public class AsyncTask {
@Async
public void task1() throws InterruptedException{
long currentTimeMillis = System.currentTimeMillis();
Thread.sleep(1000);
long currentTimeMillis1 = System.currentTimeMillis();
System.out.println("task1任務(wù)耗時(shí):"+(currentTimeMillis1-currentTimeMillis)+"ms");
}
@Async
public void task2() throws InterruptedException{
long currentTimeMillis = System.currentTimeMillis();
Thread.sleep(2000);
long currentTimeMillis1 = System.currentTimeMillis();
System.out.println("task2任務(wù)耗時(shí):"+(currentTimeMillis1-currentTimeMillis)+"ms");
}
@Async
public void task3() throws InterruptedException{
long currentTimeMillis = System.currentTimeMillis();
Thread.sleep(3000);
long currentTimeMillis1 = System.currentTimeMillis();
System.out.println("task3任務(wù)耗時(shí):"+(currentTimeMillis1-currentTimeMillis)+"ms");
}
}
3. 建立測試接口
@RestController
@RequestMapping("/test")
public class TestController {
@Autowired
private AsyncTask asyncTask;
@RequestMapping("/async")
public String doTask() throws InterruptedException{
long currentTimeMillis = System.currentTimeMillis();
asyncTask.task1();
asyncTask.task2();
asyncTask.task3();
long currentTimeMillis1 = System.currentTimeMillis();
return "task任務(wù)總耗時(shí):"+(currentTimeMillis1-currentTimeMillis)+"ms";
}
}
啟動(dòng)SpringBoot服務(wù),訪問/test/async接口,能看到任務(wù)耗時(shí)只有1s

查看控制臺(tái),發(fā)現(xiàn)異步task也成功執(zhí)行了!

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring注解@Profile實(shí)現(xiàn)開發(fā)環(huán)境/測試環(huán)境/生產(chǎn)環(huán)境的切換
在進(jìn)行軟件開發(fā)過程中,一般會(huì)將項(xiàng)目分為開發(fā)環(huán)境,測試環(huán)境,生產(chǎn)環(huán)境。本文主要介紹了Spring如何通過注解@Profile實(shí)現(xiàn)開發(fā)環(huán)境、測試環(huán)境、生產(chǎn)環(huán)境的切換,需要的可以參考一下2023-04-04
java中如何實(shí)現(xiàn) zip rar 7z 壓縮包解壓
這篇文章主要介紹了java中如何實(shí)現(xiàn) zip rar 7z 壓縮包解壓問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07
Apache Commons Math3探索之多項(xiàng)式曲線擬合實(shí)現(xiàn)代碼
這篇文章主要介紹了Apache Commons Math3探索之多項(xiàng)式曲線擬合實(shí)現(xiàn)代碼,小編覺得挺不錯(cuò)的,這里分享給大家,供需要的朋友參考。2017-10-10
SpringBoot啟動(dòng)時(shí)自動(dòng)執(zhí)行sql腳本的方法步驟
本文主要介紹了SpringBoot啟動(dòng)時(shí)自動(dòng)執(zhí)行sql腳本的方法步驟,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09
springboot 在xml里讀取yml的配置信息的示例代碼
這篇文章主要介紹了springboot 在xml里讀取yml的配置信息的示例代碼,代碼簡單易懂,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09

