springboot創(chuàng)建線程池的兩種方式小結(jié)
springboot創(chuàng)建線程池兩種方式
1.使用static代碼塊創(chuàng)建
這樣的方式創(chuàng)建的好處是當代碼用到線程池的時候才會初始化核心線程數(shù)
具體代碼如下:
public class HttpApiThreadPool {
/** 獲取當前系統(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個核心線程,線程請求個數(shù)超過20,則進入隊列等待
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)建線程池,這個是大量的生產(chǎn)事故得出來的結(jié)論
2.maximumPoolSize本程序使用的是cup數(shù)的5倍,你可以看你實際情況用
3.new ThreadFactoryBuilder().setNameFormat("PROS-%d").build() 給每個線程已名字,可以方便調(diào)試
2.使用@Configuration @bean注解,程序啟動時創(chuàng)建
@Configuration
public class TreadPoolConfig {
private Logger logger = LoggerFactory.getLogger(TreadPoolConfig.class);
/** 獲取當前系統(tǒng)的CPU 數(shù)目*/
int cpuNums = Runtime.getRuntime().availableProcessors();
/** 線程池核心池的大小*/
private int corePoolSize = 10;
/** 線程池的最大線程數(shù)*/
private int maximumPoolSize = cpuNums * 5;
/**
* 消費隊列線程
* @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如何開啟線程池
定義線程池
定義的位置,要在啟動類的子包或者同級目錄中
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ù)(默認線程數(shù))
private static final int maxPoolSize = 100; // 最大線程數(shù)
private static final int keepAliveTime = 10; // 允許線程空閑時間(單位:默認為秒)
private static final int queueCapacity = 500; // 緩沖隊列數(shù)
/**
* 默認異步線程池
* @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)用線程中運行
pool.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
// 初始化
pool.initialize();
return pool;
}
}
使用
直接在需要異步執(zhí)行的方法上加注解
@Async("taskExecutor")
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Java concurrency集合之ArrayBlockingQueue_動力節(jié)點Java學院整理
ArrayBlockingQueue是數(shù)組實現(xiàn)的線程安全的有界的阻塞隊列。下面通過本文給大家介紹Java concurrency集合之ArrayBlockingQueue的相關知識,感興趣的朋友一起看看吧2017-06-06
SpringBoot?AOP?Redis實現(xiàn)延時雙刪功能實戰(zhàn)
本文主要介紹了SpringBoot?AOP?Redis實現(xiàn)延時雙刪功能實戰(zhàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-08-08
navicatdesignquery.sql.bak系統(tǒng)找不到指定路徑錯誤的解決方法
今天小編就為大家分享一篇關于navicatdesignquery.sql.bak系統(tǒng)找不到指定路徑錯誤的解決方法,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-12-12

