激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|JavaScript|易語言|

服務器之家 - 編程語言 - Java教程 - Spring Boot 配置和使用多線程池的實現

Spring Boot 配置和使用多線程池的實現

2021-05-08 10:38阿懶土靈 Java教程

這篇文章主要介紹了Spring Boot 配置和使用多線程池的實現,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

某些情況下,我們需要在項目中對多種任務分配不同的線程池進行執行。從而通過監控不同的線程池來控制不同的任務。為了達到這個目的,需要在項目中配置多線程池。

spring boot 提供了簡單高效的線程池配置和使用方案。

配置

首先是配置線程池的bean交給spring 管理:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
@configuration
 
public class taskexecutepool {
 
 @bean(name ="threadpoola")
 
public threadpooltaskexecutormytaskasyncpool() {
 
threadpooltaskexecutor executor =new threadpooltaskexecutor();
  executor.setcorepoolsize(4);
 
  executor.setmaxpoolsize(8);
 
  executor.setqueuecapacity(100);
 
  executor.setkeepaliveseconds(60);
 
  executor.setthreadnameprefix("pool-a");
 
  executor.setrejectedexecutionhandler(new threadpoolexecutor.callerrunspolicy());
 
  executor.initialize();
 
  return executor;
 
 }
 
@bean(name ="threadpoolb")
 
public threadpooltaskexecutorasyncpoolb() {
 
threadpooltaskexecutor executor =new threadpooltaskexecutor();
 
  executor.setcorepoolsize(2);
 
  executor.setmaxpoolsize(4);
 
  executor.setqueuecapacity(8);
 
  executor.setkeepaliveseconds(60);
 
  executor.setthreadnameprefix("pool-b");
  //當任務數量超過maxpoolsize和queuecapacity時使用的策略,該策略是又調用任務的線程執行
  executor.setrejectedexecutionhandler(new threadpoolexecutor.callerrunspolicy());
 
  executor.initialize();
 
  return executor;
 
 }
 
}

使用

使用線程只需要在執行方法上加上注釋,同時該方法的類必須被定義為bean,交由spring管理。

可以在類上使用注解@component、@service等

?
1
2
3
4
@async(value="threadpoola")
public void taska(){
 ...
}

查看線程活躍數:

?
1
2
3
4
5
6
@autowired
 private threadpooltaskexecutor threadpoola;//變量名稱為定義的線程池bean定義的name屬性名。
 
public void checkavtivethreadnum() {
  int num = threadpoola.getactivecount();
}

當然還有其他一些方法,這里不再舉例。

線程池各屬性理解:

corepoolsize:表示線程池核心線程,正常情況下開啟的線程數量。

queuecapacity:當核心線程都在跑任務,還有多余的任務會存到此處。

maxpoolsize:如果queuecapacity存滿了,還有任務就會啟動更多的線程,直到線程數達到maxpoolsize。如果還有任務,則根據拒絕策略進行處理。

拒絕策略有多種:

  • 由任務調用線程執行
  • 拋異常
  • 多余的直接拋棄
  • 根據fifo(先進先出)拋棄隊列里任務

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:https://www.jianshu.com/p/0170d71dc502

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 伊人午夜视频 | 日韩在线播放第一页 | 久久免费视频1 | 91精品国产九九九久久久亚洲 | 精品国产一区二区三区天美传媒 | 欧美一级高清片 | 性生活视频软件 | 亚洲天堂在线电影 | 午夜国产小视频 | 羞羞视频免费入口网站 | 超碰97最新 | 轻点插视频| 精品国产乱码久久久久久丨区2区 | 欧美在线观看视频网站 | 国产精品久久久久久久四虎电影 | 国产一区二区在线免费观看 | 黄色免费电影网址 | 欧美视频一级 | 精品中文字幕久久久久四十五十骆 | av免播放 | av在线电影网址 | 伊人亚洲精品 | 性高湖久久久久久久久aaaaa | 欧产日产国产精品99 | 成年人观看免费视频 | 亚洲乱码精品久久久久 | 亚洲一区二区免费 | 91在线看黄 | 9999久久久久久 | 欧美成人免费看 | 中文字幕观看 | 91精品国产777在线观看 | 一级做a爱视频 | 黄色美女网站免费看 | 色老师影院 | 亚洲福利在线视频 | 欧美成人鲁丝片在线观看 | 97人人草 | 狠狠操天天操 | japanese massage tube | 国产精品久久久久久久亚洲按摩 |