SpringBoot如何實現(xiàn)并發(fā)任務并返回結果
SpringBoot并發(fā)任務并返回結果
并發(fā)的實現(xiàn)以及結果獲取
并發(fā)即多個線程同時進行任務,即異步任務,以下例子測試了并發(fā)進行四個任務,并同時返回結果的案例。
service層
@Service
public class AsyncTest {
? ? ? ? @Async
? ? ? ? public Future<Boolean> doReturn1(){
? ? ? ? ? ? try {
? ? ? ? ? ? ? ? // 這個方法需要調用500毫秒
? ? ? ? ? ? ? ? Thread.sleep(500);
? ? ? ? ? ? } catch (InterruptedException e) {
? ? ? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? }
? ? ? ? ? ? for (int i = 0; i < 10; i++) {
? ? ? ? ? ? ? ? System.out.println("一號線程:"+i);
? ? ? ? ? ? }
? ? ? ? ? ? // 消息匯總
? ? ? ? ? ? return new AsyncResult<>(true);
? ? ? ? }
? ? ? ? @Async
? ? ? ? public Future<Boolean> doReturn2(){
? ? ? ? ? ? try {
? ? ? ? ? ? ? ? // 這個方法需要調用500毫秒
? ? ? ? ? ? ? ? Thread.sleep(500);
? ? ? ? ? ? } catch (InterruptedException e) {
? ? ? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? }
? ? ? ? ? ? for (int i = 0; i < 10; i++) {
? ? ? ? ? ? ? ? System.out.println("二號線程:"+i);
? ? ? ? ? ? }
? ? ? ? ? ? // 消息匯總
? ? ? ? ? ? return new AsyncResult<>(true);
? ? ? ? }
? ? ? ? @Async
? ? ? ? public Future<Boolean> doReturn3(){
? ? ? ? ? ? try {
? ? ? ? ? ? ? ? // 這個方法需要調用500毫秒
? ? ? ? ? ? ? ? Thread.sleep(500);
? ? ? ? ? ? } catch (InterruptedException e) {
? ? ? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? }
? ? ? ? ? ? for (int i = 0; i < 10; i++) {
? ? ? ? ? ? ? ? System.out.println("三號線程:"+i);
? ? ? ? ? ? }
? ? ? ? ? ? // 消息匯總
? ? ? ? ? ? return new AsyncResult<>(true);
? ? ? ? }
? ? ? ? @Async
? ? ? ? public Future<Boolean> doReturn4(){
? ? ? ? ? ? try {
? ? ? ? ? ? ? ? // 這個方法需要調用500毫秒
? ? ? ? ? ? ? ? Thread.sleep(500);
? ? ? ? ? ? } catch (InterruptedException e) {
? ? ? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? }
? ? ? ? ? ? for (int i = 0; i < 10; i++) {
? ? ? ? ? ? ? ? System.out.println("四號線程:"+i);
? ? ? ? ? ? }
? ? ? ? ? ? // 消息匯總
? ? ? ? ? ? return new AsyncResult<>(true);
? ? ? ? }
}controller層
@RequestMapping("/async")
? ? public String async(){
? ? ? ? List<Future<Boolean>> futures = new ArrayList<>();
? ? ? ? Future<Boolean> flag1 = asyncTest.doReturn1();
? ? ? ? Future<Boolean> flag2 = asyncTest.doReturn2();
? ? ? ? Future<Boolean> flag3 = asyncTest.doReturn3();
? ? ? ? Future<Boolean> flag4 = asyncTest.doReturn4();
? ? ? ? futures.add(flag1);
? ? ? ? futures.add(flag2);
? ? ? ? futures.add(flag3);
? ? ? ? futures.add(flag4);
? ? ? ? List<Boolean> response = new ArrayList<>();
? ? ? ? for (Future future : futures) {
? ? ? ? ? ? Boolean flag = null;
? ? ? ? ? ? try {
? ? ? ? ? ? ? ? flag = (Boolean) future.get();
? ? ? ? ? ? } catch (InterruptedException e) {
? ? ? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? } catch (ExecutionException e) {
? ? ? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? }
? ? ? ? ? ? response.add(flag);
? ? ? ? }
? ? ? ? System.out.println(response);
? ? ? ? return response.toString();
? ? }這種方法即可在并發(fā)的時候攔截到全部完成后的結果進行判斷,滿足才進行下一步。
SpringBoot并發(fā)配置
Spring Boot 提供了一些默認的并發(fā)配置,可以通過配置文件或代碼進行調整。
下面介紹一些常用的 Spring Boot 并發(fā)配置:
線程池配置
可以在 application.properties 或 application.yml 文件中配置線程池的參數(shù),例如最大線程數(shù)、核心線程數(shù)等:
# application.yml spring: ? task: ? ? execution: ? ? ? pool: ? ? ? ? max-threads: 10 ? ? ? ? core-threads: 5
異步處理配置
可以通過 @EnableAsync 注解開啟異步處理,并且可以配置線程池的參數(shù):
@Configuration
@EnableAsync
public class AppConfig {
? ? @Bean
? ? public Executor taskExecutor() {
? ? ? ? ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
? ? ? ? executor.setCorePoolSize(5);
? ? ? ? executor.setMaxPoolSize(10);
? ? ? ? executor.setQueueCapacity(25);
? ? ? ? return executor;
? ? }
}WebMvc 配置
可以通過配置 WebMvcConfigurerAdapter 來配置 Spring Boot 的 MVC 框架的線程池大小和異步處理:
@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {
? ? @Override
? ? public void configureAsyncSupport(final AsyncSupportConfigurer configurer) {
? ? ? ? configurer.setTaskExecutor(asyncTaskExecutor());
? ? }
? ? @Bean
? ? public AsyncTaskExecutor asyncTaskExecutor() {
? ? ? ? ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
? ? ? ? executor.setCorePoolSize(5);
? ? ? ? executor.setMaxPoolSize(50);
? ? ? ? executor.setQueueCapacity(100);
? ? ? ? executor.setThreadNamePrefix("MySpringAsyncThread-");
? ? ? ? executor.initialize();
? ? ? ? return executor;
? ? }
}Tomcat 連接器配置
Tomcat 是 Spring Boot 的默認 Web 服務器,可以通過配置 Tomcat 的連接器參數(shù)來進行并發(fā)調優(yōu):
server: ? ? tomcat: ? ? ? ? max-connections:2000 ? ? ? ? max-threads:200 ? ? ? ? min-spare-threads:10 ? ? ? ? accept-count:100
undertow 配置
如果springboot 配置undertow作為web服務器 則可通過如下參數(shù)進行并發(fā)調優(yōu)
? # undertow 配置 ? undertow: ? ? # HTTP post內容的最大大小。當值為-1時,默認值為大小是無限的 ? ? max-http-post-size: -1 ? ? # 以下的配置會影響buffer,這些buffer會用于服務器連接的IO操作,有點類似netty的池化內存管理 ? ? # 每塊buffer的空間大小,越小的空間被利用越充分 ? ? buffer-size: 512 ? ? # 是否分配的直接內存 ? ? direct-buffers: true ? ? threads: ? ? ? # 設置IO線程數(shù), 它主要執(zhí)行非阻塞的任務,它們會負責多個連接, 默認設置每個CPU核心一個線程 ? ? ? io: 8 ? ? ? # 阻塞任務線程池, 當執(zhí)行類似servlet請求阻塞操作, undertow會從這個線程池中取得線程,它的值設置取決于系統(tǒng)的負載 ? ? ? worker: 256
其中 max-connections 表示最大連接數(shù),max-threads 表示最大線程數(shù),min-spare-threads 表示最小空閑線程數(shù),accept-count 表示請求等待隊列的大小。
綜上所述,可以通過線程池配置、異步處理配置、WebMvc 配置以及 Tomcat 連接器配置來調整 Spring Boot 應用的并發(fā)性能和并發(fā)能力。在實際應用中,需要結合具體的業(yè)務場景和系統(tǒng)架構來進行配置和優(yōu)化。
通常需要考慮硬件性能、IO模型、網(wǎng)絡、壓測、服務器監(jiān)控的實際數(shù)據(jù)。
總結
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
SpringBoot 實現(xiàn)微信掃碼登錄的示例代碼
本文主要介紹使用SpringBoot框架和微信開放平臺實現(xiàn)微信掃碼登錄的功能,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2025-10-10
springboot3整合knife4j超詳細教程(不帶swagger2玩)
本文詳細介紹了如何在Spring Boot 3項目中整合Knife4j,并提供了配置文件的詳細說明和常用注解的使用方法,感興趣的朋友跟隨小編一起看看吧2025-11-11
解決mybatis-plus3.1.1版本使用lambda表達式查詢報錯的方法
這篇文章主要介紹了解決mybatis-plus3.1.1版本使用lambda表達式查詢報錯的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-08-08
創(chuàng)建Java keystore文件的完整指南及詳細步驟
本文詳解Java中keystore的創(chuàng)建與配置,涵蓋私鑰管理、自簽名與CA證書生成、SSL/TLS應用,強調安全存儲及驗證機制,確保通信加密和數(shù)據(jù)完整性,感興趣的朋友一起看看吧2025-07-07
Spring框架的環(huán)境搭建和測試實現(xiàn)
這篇文章主要介紹了Spring框架的環(huán)境搭建和測試實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-10-10
解決StringBuffer和StringBuilder的擴容問題
這篇文章主要介紹了解決StringBuffer和StringBuilder的擴容問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07
從 Spring Boot 3+Java 21 到 Spri
本文介紹了從SpringBoot3+Java21升級到SpringBoot4+Java25的具體步驟與注意事項,主要包含環(huán)境與依賴自查、代碼適配、進階優(yōu)化、測試與驗證以及遷移checklist等內容,感興趣的朋友跟隨小編一起看看吧2026-04-04

