網站首頁 編程語言 正文
?? ? ? ? 首先是線程池的配置類如下:
@Configuration
public class ThreadPoolHandle {
@Value("${single.threadPool.core}")
private Integer core;
@Value("${single.threadPool.max}")
private Integer max;
@Value("${single.threadPool.keepLive}")
private long keepLive;
@Value("${single.threadPool.queue}")
private Integer queue;
@Bean
public ThreadPoolExecutor geniusThreadPool(){
//創建線程池
ThreadPoolExecutor threadPool = new ThreadPoolExecutor(
//線程池中的常駐核心線程數(假設銀行窗口一般情況下開兩個)
core,
//線程池能夠容納同時執行的最大線程數,此值必須>=1(特殊情況下比如周末辦理業務人多,增設到5個窗口)
max,
//多余的空閑線程的存活時間 當前線程池數量超過 "corepoolsize"(認為有問題 應該是超過 maximumpoolsize吧)
keepLive,
// 當空閑時間達到keepalivetime值時,多余空閑線程會被銷毀直到只剩下 corePoolSize個線程為止
//unit keepAliveTime的單位
TimeUnit.SECONDS,
//任務隊列,被提交但尚未被執行的任務(銀行窗口的等待區的座位)
new LinkedBlockingQueue<>(queue),
//線程工廠,用于生成線程池中的工作線程
new XXXXThreadFactory("dataHandlePool"),
//拒絕策略,當任務隊列[LindedBlockingQueue]滿了,
// 并且工作線程[自己理解為辦理業務的總認識,如以下代碼中的10]大于等于線程池的最大線程數時如何拒絕多余工作線程
new ThreadPoolExecutor.AbortPolicy()
);
threadPool.allowCoreThreadTimeOut(true);
return threadPool;
}
? ? ? ? yml中的配置如下:
single:
threadPool:
core: 8
max: 18
keepLive: 10
queue: 100
? ? ? ? 線程工廠如下:
public class AtfRiskThreadFactory implements ThreadFactory {
private static final AtomicInteger poolNumber = new AtomicInteger(1);
private final ThreadGroup group;
private final AtomicInteger threadNumber = new AtomicInteger(1);
private final String namePrefix;
AtfRiskThreadFactory(String name) {
SecurityManager s = System.getSecurityManager();
group = (s != null) ? s.getThreadGroup() :
Thread.currentThread().getThreadGroup();
if(name == null || "".equals(name.trim())){
name = "pool";
}
namePrefix = name + "-" +
poolNumber.getAndIncrement() +
"-thread-";
}
@Override
public Thread newThread(Runnable r) {
Thread t = new Thread(group, r,
namePrefix + threadNumber.getAndIncrement(),
0);
if (t.isDaemon()){
t.setDaemon(false);
}
if (t.getPriority() != Thread.NORM_PRIORITY){
t.setPriority(Thread.NORM_PRIORITY);
}
return t;
}
}
? ? ? ? 線程池的使用:
try{
threadPool.execute(()->{
System.out.println(Thread.currentThread().getName()+" "+ finalI +" 辦理業務");
});
}catch (RejectedExecutionException e){
System.out.println("捕獲到異常===================->");
System.out.println("把消息:"+finalI+"保存起來");
}
原文鏈接:https://blog.csdn.net/m0_65014849/article/details/129534054
- 上一篇:沒有了
- 下一篇:沒有了
相關推薦
- 2023-07-02 一文詳解Python中logging模塊的用法_python
- 2022-04-19 前端如何使用webpack優化性能
- 2022-07-24 docker容器使用GPU方法實現_docker
- 2023-02-17 C++可執行文件絕對路徑值與VS安全檢查詳解_C 語言
- 2023-01-03 python實現線性插值的示例_python
- 2023-03-28 react-redux及redux狀態管理工具使用詳解_React
- 2022-12-03 C++時間函數整理詳解_C 語言
- 2022-08-02 Android開發自定義雙向SeekBar拖動條控件_Android
- 欄目分類
-
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支