自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

詳解SpringBoot中的異步調用@Async

開發(fā) 前端
本篇帶給大家SpringBoot中的異步調用@Async,在SpringBoot中,只需要給方法加上@Async注解,就能將同步方法變?yōu)楫惒秸{用。

[[390141]]

如何開啟異步調用

在SpringBoot中,只需要給方法加上@Async注解,就能將同步方法變?yōu)楫惒秸{用。

首先在啟動類上添加@EnableAsync,即開啟異步調用。

  1. /** 
  2.  * @author qcy 
  3.  */ 
  4. @SpringBootApplication 
  5. @EnableAsync 
  6. public class AsyncApplication { 
  7.  
  8.     public static void main(String[] args) { 
  9.         SpringApplication.run(AsyncApplication.class, args); 
  10.     } 
  11.  

在需要異步調用的方法上加上@Async注解

  1. package com.yang.async; 
  2.  
  3. import lombok.extern.slf4j.Slf4j; 
  4. import org.springframework.scheduling.annotation.Async; 
  5. import org.springframework.scheduling.annotation.AsyncResult; 
  6. import org.springframework.stereotype.Component; 
  7.  
  8. import java.util.concurrent.Future; 
  9. import java.util.concurrent.FutureTask; 
  10.  
  11. /** 
  12.  * @author qcy 
  13.  * @create 2020/09/09 14:01:35 
  14.  */ 
  15. @Slf4j 
  16. @Component 
  17. public class Task { 
  18.  
  19.     @Async 
  20.     public void method1() { 
  21.         log.info("method1開始,執(zhí)行線程為" + Thread.currentThread().getName()); 
  22.         try { 
  23.             Thread.sleep(2000); 
  24.         } catch (InterruptedException e) { 
  25.             e.printStackTrace(); 
  26.         } 
  27.         log.info("method1結束"); 
  28.     } 
  29.  
  30.     @Async 
  31.     public void method2() { 
  32.         log.info("method2開始,執(zhí)行線程為" + Thread.currentThread().getName()); 
  33.         try { 
  34.             Thread.sleep(3000); 
  35.         } catch (InterruptedException e) { 
  36.             e.printStackTrace(); 
  37.         } 
  38.         log.info("method2結束"); 
  39.     } 
  40.  
  41.  

 測試一下:

  1. @SpringBootTest 
  2. @Slf4j 
  3. public class AsyncApplicationTests { 
  4.  
  5.     @Autowired 
  6.     Task task; 
  7.  
  8.     @Test 
  9.     public void testAsyncWithVoidReturn() throws InterruptedException { 
  10.         log.info("main線程開始"); 
  11.  
  12.         task.method1(); 
  13.         task.method2(); 
  14.  
  15.         //確保兩個異步調用執(zhí)行完成 
  16.         Thread.sleep(6000); 
  17.  
  18.         log.info("main線程結束"); 
  19.     } 
  20.  

 輸出如下:

可以看得出,SpringBoot創(chuàng)建了一個名為applicationTaskExecutor的線程池,使用這里面的線程來執(zhí)行異步調用。

這里值得注意的是,不要在一個類中調用@Async標注的方法,否則不會起到異步調用的作用,至于為什么會產生這樣的問題,需要深入到源碼中一探究竟,會另開篇幅。

既然默認使用的是SpringBoot自己創(chuàng)建的applicationTaskExecutor,那如何自己去定義一個線程池呢?

自定義線程池

我們需要手動創(chuàng)建一個名為asynTaskExecutord的Bean

  1. package com.yang.async; 
  2.  
  3. import lombok.extern.slf4j.Slf4j; 
  4. import org.springframework.context.annotation.Bean; 
  5. import org.springframework.context.annotation.Configuration; 
  6. import org.springframework.core.task.AsyncTaskExecutor; 
  7. import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; 
  8.  
  9. import java.util.concurrent.ThreadPoolExecutor; 
  10.  
  11. /** 
  12.  * @author qcy 
  13.  * @create 2020/09/09 15:31:07 
  14.  */ 
  15. @Slf4j 
  16. @Configuration 
  17. public class AsyncConfig { 
  18.  
  19.     @Bean 
  20.     public AsyncTaskExecutor asyncTaskExecutor() { 
  21.         ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); 
  22.         executor.setCorePoolSize(8); 
  23.         executor.setMaxPoolSize(16); 
  24.         executor.setQueueCapacity(50); 
  25.         executor.setAllowCoreThreadTimeOut(true); 
  26.         executor.setKeepAliveSeconds(10); 
  27.         executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); 
  28.         executor.setThreadNamePrefix("async-thread-pool-thread"); 
  29.         return executor; 
  30.     } 

 對以上參數(shù)不了解的同學,可以參考我的這篇文章說說線程池

其他類不需要變動,直接運行剛才的testAsyncWithVoidReturn()方法,輸出:

看得出來,現(xiàn)在是我們自定義的線程池

如果關心異步調用的返回值,又怎么處理?

獲取異步調用的返回結果

獲取異步調用的結果,需要利用Future機制,可以參考我的另外一篇文章談談Runnable、Future、Callable、FutureTask之間的關系

為Task類增加以下兩個方法:

  1. @Async 
  2.   public Future<String> method3() { 
  3.       log.info("method3開始,執(zhí)行線程為" + Thread.currentThread().getName()); 
  4.       try { 
  5.           Thread.sleep(1000); 
  6.       } catch (InterruptedException e) { 
  7.           e.printStackTrace(); 
  8.       } 
  9.       log.info("method3結束"); 
  10.       return new AsyncResult<>("method3"); 
  11.   } 
  12.  
  13.   @Async 
  14.   public Future<String> method4() { 
  15.       log.info("method4開始,執(zhí)行線程為" + Thread.currentThread().getName()); 
  16.       try { 
  17.           Thread.sleep(3000); 
  18.       } catch (InterruptedException e) { 
  19.           e.printStackTrace(); 
  20.       } 
  21.       log.info("method4結束"); 
  22.       return new AsyncResult<>("method4"); 
  23.   } 

 測試類:

  1. @Test 
  2.   public void testAsyncWithStringReturn() throws InterruptedException, ExecutionException { 
  3.       log.info("main線程開始"); 
  4.  
  5.       Future<String> method3Result = task.method3(); 
  6.       Future<String> method4Result = task.method4(); 
  7.  
  8.       //get方法為阻塞獲取 
  9.       log.info("method3執(zhí)行的返回結果:{}", method3Result.get()); 
  10.       log.info("method4執(zhí)行的返回結果:{}", method4Result.get()); 
  11.       log.info("main線程結束"); 
  12.   } 

 輸出:

如圖,在主線程結束前,獲取到了異步調用的結果。且在兩個異步調用都結束的情況下,繼續(xù)執(zhí)行主線程。

 

責任編輯:姜華 來源: 今日頭條
相關推薦

2024-07-31 15:57:41

2024-10-15 10:28:43

2010-01-11 17:24:19

VB.NET異步調用

2009-10-20 16:48:30

C#委托

2018-06-21 14:46:03

Spring Boot異步調用

2023-08-23 13:24:00

異步編程方法

2021-03-19 10:14:28

SpringBoot項目異步調用

2022-09-27 12:01:56

Spring異步調用方式

2009-11-09 10:50:30

WCF異步調用

2009-12-21 14:10:26

WCF異步調用

2009-08-21 13:18:25

C#異步調用

2009-07-01 14:23:46

JavaScript異

2009-07-01 14:37:14

JavaScript異

2009-07-01 14:31:01

JavaScript異

2009-11-06 15:54:15

WCF異步調用

2009-07-01 13:58:00

JavaScript異

2022-09-28 14:54:07

Spring注解方式線程池

2022-07-01 08:14:28

Dubbo異步代碼

2009-08-21 11:02:55

C#異步調用

2012-10-29 10:59:27

Windows 8
點贊
收藏

51CTO技術棧公眾號