springboot創(chuàng)建線程池的兩種方式小結(jié)
springboot創(chuàng)建線程池兩種方式
1.使用static代碼塊創(chuàng)建
這樣的方式創(chuàng)建的好處是當(dāng)代碼用到線程池的時(shí)候才會(huì)初始化核心線程數(shù)
具體代碼如下:
public class HttpApiThreadPool { /** 獲取當(dāng)前系統(tǒng)的CPU 數(shù)目*/ static int cpuNums = Runtime.getRuntime().availableProcessors(); /** 線程池核心池的大小*/ private static int corePoolSize = 10; /** 線程池的最大線程數(shù)*/ private static int maximumPoolSize = cpuNums * 5; public static ExecutorService httpApiThreadPool = null; /** * 靜態(tài)方法 */ static{ System.out.println("創(chuàng)建線程數(shù):"+corePoolSize+",最大線程數(shù):"+maximumPoolSize); //建立10個(gè)核心線程,線程請求個(gè)數(shù)超過20,則進(jìn)入隊(duì)列等待 httpApiThreadPool = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, 0L, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(100),new ThreadFactoryBuilder().setNameFormat("PROS-%d").build()); } }
使用方法
public static void main(String[] args) { HttpApiThreadPool.httpApiThreadPool.execute(()->System.out.println("測試")); }
注意:
1.不能使用Executors的方法創(chuàng)建線程池,這個(gè)是大量的生產(chǎn)事故得出來的結(jié)論
2.maximumPoolSize本程序使用的是cup數(shù)的5倍,你可以看你實(shí)際情況用
3.new ThreadFactoryBuilder().setNameFormat("PROS-%d").build() 給每個(gè)線程已名字,可以方便調(diào)試
2.使用@Configuration @bean注解,程序啟動(dòng)時(shí)創(chuàng)建
@Configuration public class TreadPoolConfig { private Logger logger = LoggerFactory.getLogger(TreadPoolConfig.class); /** 獲取當(dāng)前系統(tǒng)的CPU 數(shù)目*/ int cpuNums = Runtime.getRuntime().availableProcessors(); /** 線程池核心池的大小*/ private int corePoolSize = 10; /** 線程池的最大線程數(shù)*/ private int maximumPoolSize = cpuNums * 5; /** * 消費(fèi)隊(duì)列線程 * @return */ @Bean(value = "httpApiThreadPool") public ExecutorService buildHttpApiThreadPool(){ logger.info("TreadPoolConfig創(chuàng)建線程數(shù):"+corePoolSize+",最大線程數(shù):"+maximumPoolSize); ExecutorService pool = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, 0L, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(100),new ThreadFactoryBuilder().setNameFormat("PROS-%d").build()); return pool ; } }
使用方法
//注入 @Resource private TreadPoolConfig treadPoolConfig; //調(diào)用 public void test() { treadPoolConfig.buildHttpApiThreadPool().execute(()->System.out.println("tre")); }
現(xiàn)在兩種線程池的創(chuàng)建方法已經(jīng)介紹完了。
springboot如何開啟線程池
定義線程池
定義的位置,要在啟動(dòng)類的子包或者同級(jí)目錄中
import lombok.Data; 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.ThreadPoolExecutor; @Data @Configuration @EnableAsync //開啟異步請求 public class ThreadPoolConfig { private static final int corePoolSize = 10; // 核心線程數(shù)(默認(rèn)線程數(shù)) private static final int maxPoolSize = 100; // 最大線程數(shù) private static final int keepAliveTime = 10; // 允許線程空閑時(shí)間(單位:默認(rèn)為秒) private static final int queueCapacity = 500; // 緩沖隊(duì)列數(shù) /** * 默認(rèn)異步線程池 * @return */ @Bean("taskExecutor") public ThreadPoolTaskExecutor taskExecutor(){ ThreadPoolTaskExecutor pool = new ThreadPoolTaskExecutor(); pool.setThreadNamePrefix("--------------全局線程池-----------------"); pool.setCorePoolSize(corePoolSize); pool.setMaxPoolSize(maxPoolSize); pool.setKeepAliveSeconds(keepAliveTime); pool.setQueueCapacity(queueCapacity); // 直接在execute方法的調(diào)用線程中運(yùn)行 pool.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); // 初始化 pool.initialize(); return pool; } }
使用
直接在需要異步執(zhí)行的方法上加注解
@Async("taskExecutor")
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Maven項(xiàng)目web多圖片上傳及格式驗(yàn)證的實(shí)現(xiàn)
本文主要介紹了Maven項(xiàng)目web多圖片上傳及格式驗(yàn)證的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10解決異常FileNotFoundException:class path resource找不到資源文件的問題
今天小編就為大家分享一篇關(guān)于解決異常FileNotFoundException:class path resource找不到資源文件的問題,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2018-12-12springboot 如何解決static調(diào)用service為null
這篇文章主要介紹了springboot 如何解決static調(diào)用service為null的問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06java反射之Method的invoke方法實(shí)現(xiàn)教程詳解
這篇文章主要給大家介紹了關(guān)于java反射之Method的invoke方法的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01Java微信公眾平臺(tái)開發(fā)(15) 微信JSSDK的使用
這篇文章主要為大家詳細(xì)介紹了Java微信公眾平臺(tái)開發(fā)第十五步,微信JSSDK的使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04SpringCloud啟動(dòng)eureka server后,沒報(bào)錯(cuò)卻不能訪問管理頁面(404問題)
這篇文章主要介紹了SpringCloud啟動(dòng)eureka server后,沒報(bào)錯(cuò)卻不能訪問管理頁面(404問題),具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11Hibernate用ThreadLocal模式(線程局部變量模式)管理Session
今天小編就為大家分享一篇關(guān)于Hibernate用ThreadLocal模式(線程局部變量模式)管理Session,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-03-03Java7和Java8中的ConcurrentHashMap原理解析
這篇文章主要介紹了Java7和Java8中的ConcurrentHashMap原理解析,對ConcurrentHashMap感興趣的讀者,一定要好好看一下2021-04-04