Springboot實(shí)現(xiàn)多線程及線程池監(jiān)控
線程池的優(yōu)點(diǎn)
- 降低資源消耗。通過(guò)重復(fù)利用已創(chuàng)建的線程降低線程創(chuàng)建和銷毀造成的消耗
- 提高響應(yīng)速度。當(dāng)任務(wù)到達(dá)時(shí),任務(wù)可以不需要等到線程創(chuàng)建就能立即執(zhí)行
- 可以對(duì)線程做統(tǒng)一管理。
1.配置線程池
修改配置文件
# 異步線程配置 # 配置核心線程數(shù) async: executor: thread: core_pool_size: 5 # 配置核心線程數(shù) max_pool_size: 5 # 配置最大線程數(shù) queue_capacity: 99999 # 配置隊(duì)列大小 name: prefix: async-service- # 配置線程池中的線程的名稱前
線程池配置類
package com.bt.springboot.config; import com.bt.springboot.task.ThreadPoolMonitor; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import java.util.concurrent.Executor; import java.util.concurrent.ThreadPoolExecutor; /** * @author zkx * @Date 2022/12/6 21:42 */ @Slf4j @Configuration @EnableAsync public class ExecutorConfig { @Value("${async.executor.thread.core_pool_size}") private int corePoolSize; @Value("${async.executor.thread.max_pool_size}") private int maxPoolSize; @Value("${async.executor.thread.queue_capacity}") private int queueCapacity; @Value("${async.executor.thread.name.prefix}") private String namePrefix; @Bean(name = "asyncServiceExecutor") public Executor asyncServiceExecutor() { log.info("start asyncServiceExecutor"); ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); //配置核心線程數(shù) executor.setCorePoolSize(corePoolSize); //配置最大線程數(shù) executor.setMaxPoolSize(maxPoolSize); //配置隊(duì)列大小 executor.setQueueCapacity(queueCapacity); //配置線程池中的線程的名稱前綴 executor.setThreadNamePrefix(namePrefix); // rejection-policy:當(dāng)pool已經(jīng)達(dá)到max size的時(shí)候,如何處理新任務(wù) // CALLER_RUNS:不在新線程中執(zhí)行任務(wù),而是有調(diào)用者所在的線程來(lái)執(zhí)行 executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); //執(zhí)行初始化 executor.initialize(); return executor; } }
2.監(jiān)控類
package com.bt.springboot.task; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import org.springframework.stereotype.Component; import java.util.concurrent.ThreadPoolExecutor; /** * @Author: ChenBin */ @Slf4j @Component public class ThreadPoolMonitor extends ThreadPoolTaskExecutor { @Value("${async.executor.thread.name.prefix}") private String namePrefix; @Scheduled(cron = "0/1 * * * * ? ") private void showThreadPoolInfo() { ThreadPoolExecutor threadPoolExecutor = getThreadPoolExecutor(); if (namePrefix.equals(this.getThreadNamePrefix())){ log.info("{} taskCount [{}], completedTaskCount [{}], activeCount [{}], queueSize [{}]", this.getThreadNamePrefix(), threadPoolExecutor.getTaskCount(), threadPoolExecutor.getCompletedTaskCount(), threadPoolExecutor.getActiveCount(), threadPoolExecutor.getQueue().size()); } } }
開啟定時(shí)任務(wù)
3.修改線程池配置類
將ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
修改為ThreadPoolTaskExecutor executor = new ThreadPoolMonitor();
4.測(cè)試相關(guān)類
AsyncService
package com.bt.springboot.async; /** * @author zkx * @Date 2022/12/6 21:47 */ public interface AsyncService { /** * 執(zhí)行異步任務(wù) * 可以根據(jù)需求,自己加參數(shù)擬定,我這里就做個(gè)測(cè)試演示 */ void executeAsync(); }
AsyncServiceImpl
package com.bt.springboot.async.impl; import com.bt.springboot.async.AsyncService; import lombok.extern.slf4j.Slf4j; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; /** * @author zkx * @Date 2022/12/6 21:48 */ @Slf4j @Service public class AsyncServiceImpl implements AsyncService { @Override @Async("asyncServiceExecutor") public void executeAsync() { int i = 5; while(i > 0){ i--; log.info("execute task"); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); Thread.currentThread().interrupt(); } } } }
AsyncController
package com.bt.springboot.web.controller; import com.bt.springboot.async.AsyncService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; /** * @author zkx * @Date 2022/12/9 17:38 */ @RestController public class AsyncController { @Autowired private AsyncService asyncService; @GetMapping("/asyncTask") public void asyncTask(){ asyncService.executeAsync(); } }
5.啟動(dòng)
1.執(zhí)行任務(wù)前
2.執(zhí)行任務(wù)
3.任務(wù)完成
到此這篇關(guān)于Springboot實(shí)現(xiàn)多線程及線程池監(jiān)控的文章就介紹到這了,更多相關(guān)Springboot 多線程及線程池監(jiān)控內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解spring cloud整合Swagger2構(gòu)建RESTful服務(wù)的APIs
這篇文章主要介紹了詳解spring cloud整合Swagger2構(gòu)建RESTful服務(wù)的APIs,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-01-01PowerJob的GridFsManager工作流程源碼解讀
這篇文章主要為大家介紹了PowerJob的GridFsManager工作流程源碼解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01JAVA?IDEA項(xiàng)目打包為jar包的步驟詳解
在Java開發(fā)中我們通常會(huì)將我們的項(xiàng)目打包成可執(zhí)行的Jar包,以便于在其他環(huán)境中部署和運(yùn)行,下面這篇文章主要給大家介紹了關(guān)于JAVA?IDEA項(xiàng)目打包為jar包的相關(guān)資料,需要的朋友可以參考下2024-08-08springboot集成gzip和zip數(shù)據(jù)壓縮傳輸(適用大數(shù)據(jù)信息傳輸)
?在大數(shù)據(jù)量的傳輸中,壓縮數(shù)據(jù)后進(jìn)行傳輸可以一定程度的解決速度問(wèn)題,本文主要介紹了springboot集成gzip和zip數(shù)據(jù)壓縮傳輸,具有一定的參考價(jià)值,感興趣的可以了解一下2023-09-09Springboot繼承Keycloak實(shí)現(xiàn)單點(diǎn)登錄與退出功能
這篇文章主要介紹了Springboot繼承Keycloak實(shí)現(xiàn)單點(diǎn)登陸與退出,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-08-08