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è)核心線程,線程請(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("測(cè)試"));
}
注意:
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 //開啟異步請(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")
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
java多線程累加計(jì)數(shù)的實(shí)現(xiàn)方法
在多線程協(xié)作任務(wù)中,如何計(jì)算也是很重的,這篇文章主要介紹了java多線程累加計(jì)數(shù)的實(shí)現(xiàn)方法,感興趣的朋友可以了解一下2021-05-05
Java concurrency集合之ArrayBlockingQueue_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
ArrayBlockingQueue是數(shù)組實(shí)現(xiàn)的線程安全的有界的阻塞隊(duì)列。下面通過本文給大家介紹Java concurrency集合之ArrayBlockingQueue的相關(guān)知識(shí),感興趣的朋友一起看看吧2017-06-06
Java中使用instanceof判斷對(duì)象類型的示例
在List<Object>中遍歷Object時(shí),先判斷類型,再定向轉(zhuǎn)換,本文給大家介紹Java中使用instanceof判斷對(duì)象類型,感興趣的朋友跟隨小編一起看看吧2023-08-08
SpringBoot?AOP?Redis實(shí)現(xiàn)延時(shí)雙刪功能實(shí)戰(zhàn)
本文主要介紹了SpringBoot?AOP?Redis實(shí)現(xiàn)延時(shí)雙刪功能實(shí)戰(zhàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08
navicatdesignquery.sql.bak系統(tǒng)找不到指定路徑錯(cuò)誤的解決方法
今天小編就為大家分享一篇關(guān)于navicatdesignquery.sql.bak系統(tǒng)找不到指定路徑錯(cuò)誤的解決方法,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2018-12-12

