您好,登錄后才能下訂單哦!
這篇文章主要介紹springboot如何創(chuàng)建線程池,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
這樣的方式創(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è)核心線程,線程請(qǐng)求個(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)試
@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)介紹完了。
定義的位置,要在啟動(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 //開啟異步請(qǐng)求 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")
以上是“springboot如何創(chuàng)建線程池”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。