java 注解實(shí)現(xiàn)一個(gè)可配置線程池的方法示例
前言
項(xiàng)目需要多線程執(zhí)行一些Task,為了方便各個(gè)服務(wù)的使用。特意封裝了一個(gè)公共工具類,下面直接擼代碼:
PoolConfig(線程池核心配置參數(shù)):
/**
* <h1>線程池核心配置(<b style="color:#CD0000">基本線程池?cái)?shù)量、最大線程池?cái)?shù)量、隊(duì)列初始容量、線程連接保持活動秒數(shù)(默認(rèn)60s)</b>)</h1>
*
* <blockquote><code>
* <table border="1px" style="border-color:gray;" width="100%"><tbody>
* <tr><th style="color:green;text-align:left;">
* 屬性名稱
* </th><th style="color:green;text-align:left;">
* 屬性含義
* </th></tr>
* <tr><td>
* queueCapacity
* </td><td>
* 基本線程池?cái)?shù)量
* </td></tr>
* <tr><td>
* count
* </td><td>
* 最大線程池?cái)?shù)量
* </td></tr>
* <tr><td>
* maxCount
* </td><td>
* 隊(duì)列初始容量
* </td></tr>
* <tr><td>
* aliveSec
* </td><td>
* 線程連接保持活動秒數(shù)(默認(rèn)60s)
* </td></tr>
* </tbody></table>
* </code></blockquote>
*/
public class PoolConfig {
private int queueCapacity = 200;
private int count = 0;
private int maxCount = 0;
private int aliveSec;
public int getQueueCapacity() {
return queueCapacity;
}
public void setQueueCapacity(int queueCapacity) {
this.queueCapacity = queueCapacity;
}
public void setCount(int count) {
this.count = count;
}
public void setMaxCount(int maxCount) {
this.maxCount = maxCount;
}
public void setAliveSec(int aliveSec) {
this.aliveSec = aliveSec;
}
public int getCount() {
return count;
}
public int getMaxCount() {
return maxCount;
}
public int getAliveSec() {
return aliveSec;
}
}
ThreadPoolConfig(線程池配置 yml配置項(xiàng)以thread開頭):
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* <h1>線程池配置(<b style="color:#CD0000">線程池核心配置、各個(gè)業(yè)務(wù)處理的任務(wù)數(shù)量</b>)</h1>
*
* <blockquote><code>
* <table border="1px" style="border-color:gray;" width="100%"><tbody>
* <tr><th style="color:green;text-align:left;">
* 屬性名稱
* </th><th style="color:green;text-align:left;">
* 屬性含義
* </th></tr>
* <tr><td>
* pool
* </td><td>
* 線程池核心配置
* 【{@link PoolConfig}】
* </td></tr>
* <tr><td>
* count
* </td><td>
* 線程池各個(gè)業(yè)務(wù)任務(wù)初始的任務(wù)數(shù)
* </td></tr>
* </tbody></table>
* </code></blockquote>
*/
@Component
@ConfigurationProperties(prefix="thread")
public class ThreadPoolConfig {
private PoolConfig pool = new PoolConfig();
Map<String, Integer> count = new HashMap<>();
public PoolConfig getPool() {
return pool;
}
public void setPool(PoolConfig pool) {
this.pool = pool;
}
public Map<String, Integer> getCount() {
return count;
}
}
定義Task注解,方便使用:
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface ExcutorTask {
/**
* The value may indicate a suggestion for a logical ExcutorTask name,
* to be turned into a Spring bean in case of an autodetected ExcutorTask .
* @return the suggested ExcutorTask name, if any
*/
String value() default "";
}
通過反射獲取使用Task注解的任務(wù)集合:
public class Beans {
private static final char PREFIX = '.';
public static ConcurrentMap<String, String> scanBeanClassNames(){
ConcurrentMap<String, String> beanClassNames = new ConcurrentHashMap<>();
ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(false);
provider.addIncludeFilter(new AnnotationTypeFilter(ExcutorTask.class));
for(Package pkg : Package.getPackages()){
String basePackage = pkg.getName();
Set<BeanDefinition> components = provider.findCandidateComponents(basePackage);
for (BeanDefinition component : components) {
String beanClassName = component.getBeanClassName();
try {
Class<?> clazz = Class.forName(component.getBeanClassName());
boolean isAnnotationPresent = clazz.isAnnotationPresent(ZimaTask.class);
if(isAnnotationPresent){
ZimaTask task = clazz.getAnnotation(ExcutorTask.class);
String aliasName = task.value();
if(aliasName != null && !"".equals(aliasName)){
beanClassNames.put(aliasName, component.getBeanClassName());
}
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
beanClassNames.put(beanClassName.substring(beanClassName.lastIndexOf(PREFIX) + 1), component.getBeanClassName());
}
}
return beanClassNames;
}
}
線程執(zhí)行類TaskPool:
@Component
public class TaskPool {
public ThreadPoolTaskExecutor poolTaskExecutor;
@Autowired
private ThreadPoolConfig threadPoolConfig;
@Autowired
private ApplicationContext context;
private final Integer MAX_POOL_SIZE = 2000;
private PoolConfig poolCfg;
private Map<String, Integer> tasksCount;
private ConcurrentMap<String, String> beanClassNames;
@PostConstruct
public void init() {
beanClassNames = Beans.scanBeanClassNames();
poolTaskExecutor = new ThreadPoolTaskExecutor();
poolCfg = threadPoolConfig.getPool();
tasksCount = threadPoolConfig.getCount();
int corePoolSize = poolCfg.getCount(),
maxPoolSize = poolCfg.getMaxCount(),
queueCapacity = poolCfg.getQueueCapacity(),
minPoolSize = 0, maxCount = (corePoolSize << 1);
for(String taskName : tasksCount.keySet()){
minPoolSize += tasksCount.get(taskName);
}
if(corePoolSize > 0){
if(corePoolSize <= minPoolSize){
corePoolSize = minPoolSize;
}
}else{
corePoolSize = minPoolSize;
}
if(queueCapacity > 0){
poolTaskExecutor.setQueueCapacity(queueCapacity);
}
if(corePoolSize > 0){
if(MAX_POOL_SIZE < corePoolSize){
corePoolSize = MAX_POOL_SIZE;
}
poolTaskExecutor.setCorePoolSize(corePoolSize);
}
if(maxPoolSize > 0){
if(maxPoolSize <= maxCount){
maxPoolSize = maxCount;
}
if(MAX_POOL_SIZE < maxPoolSize){
maxPoolSize = MAX_POOL_SIZE;
}
poolTaskExecutor.setMaxPoolSize(maxPoolSize);
}
if(poolCfg.getAliveSec() > 0){
poolTaskExecutor.setKeepAliveSeconds(poolCfg.getAliveSec());
}
poolTaskExecutor.initialize();
}
public void execute(Class<?>... clazz){
int i = 0, len = tasksCount.size();
for(; i < len; i++){
Integer taskCount = tasksCount.get(i);
for(int t = 0; t < taskCount; t++){
try{
Object taskObj = context.getBean(clazz[i]);
if(taskObj != null){
poolTaskExecutor.execute((Runnable) taskObj);
}
}catch(Exception ex){
ex.printStackTrace();
}
}
}
}
public void execute(String... args){
int i = 0, len = tasksCount.size();
for(; i < len; i++){
Integer taskCount = tasksCount.get(i);
for(int t = 0; t < taskCount; t++){
try{
Object taskObj = null;
if(context.containsBean(args[i])){
taskObj = context.getBean(args[i]);
}else{
if(beanClassNames.containsKey(args[i].toLowerCase())){
Class<?> clazz = Class.forName(beanClassNames.get(args[i].toLowerCase()));
taskObj = context.getBean(clazz);
}
}
if(taskObj != null){
poolTaskExecutor.execute((Runnable) taskObj);
}
}catch(Exception ex){
ex.printStackTrace();
}
}
}
}
public void execute(){
for(String taskName : tasksCount.keySet()){
Integer taskCount = tasksCount.get(taskName);
for(int t = 0; t < taskCount; t++){
try{
Object taskObj = null;
if(context.containsBean(taskName)){
taskObj = context.getBean(taskName);
}else{
if(beanClassNames.containsKey(taskName)){
Class<?> clazz = Class.forName(beanClassNames.get(taskName));
taskObj = context.getBean(clazz);
}
}
if(taskObj != null){
poolTaskExecutor.execute((Runnable) taskObj);
}
}catch(Exception ex){
ex.printStackTrace();
}
}
}
}
}
如何使用?(做事就要做全套 ^_^)
1.因?yàn)槭褂玫膕pringboot項(xiàng)目,需要在application.properties 或者 application.yml 添加
#配置執(zhí)行的task線程數(shù) thread.count.NeedExcutorTask=4 #最大存活時(shí)間 thread.pool.aliveSec=300000 #其他配置同理
2.將我們寫的線程配置進(jìn)行裝載到我們的項(xiàng)目中
@Configuration
public class TaskManager {
@Resource
private TaskPool taskPool;
@PostConstruct
public void executor(){
taskPool.execute();
}
}
3.具體使用
@ExcutorTask
public class NeedExcutorTask implements Runnable{
@Override
public void run() {
Thread.sleep(1000L);
log.info("====== 任務(wù)執(zhí)行 =====")
}
}
以上就是創(chuàng)建一個(gè)可擴(kuò)展的線程池相關(guān)的配置(望指教~~~)。希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Tk.mybatis零sql語句實(shí)現(xiàn)動態(tài)sql查詢的方法(4種)
有時(shí)候,查詢數(shù)據(jù)需要根據(jù)條件使用動態(tài)查詢,這時(shí)候需要使用動態(tài)sql,本文主要介紹了Tk.mybatis零sql語句實(shí)現(xiàn)動態(tài)sql查詢的方法,感興趣的可以了解一下2021-12-12
Spring AOP結(jié)合注解實(shí)現(xiàn)接口層操作日志記錄
在項(xiàng)目開發(fā)中我們需要記錄接口的操作日志:包含請求參數(shù)、響應(yīng)參數(shù)、接口所屬模塊、接口功能描述、請求地址、ip地址等信息;實(shí)現(xiàn)思路很簡單就是基于注解和aop的方式去記錄日志,主要的難點(diǎn)在于日志表結(jié)構(gòu)、注解的設(shè)計(jì)已經(jīng)aop實(shí)現(xiàn)的一些比較好的實(shí)現(xiàn)方式的借鑒2022-08-08
Mybatis逆向生成使用擴(kuò)展類的實(shí)例代碼詳解
這篇文章主要介紹了Mybatis逆向生成使用擴(kuò)展類的實(shí)例代碼,代碼簡單易懂,非常不錯,具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下2019-05-05
Java 實(shí)戰(zhàn)項(xiàng)目錘煉之嘟嘟健身房管理系統(tǒng)的實(shí)現(xiàn)流程
讀萬卷書不如行萬里路,只學(xué)書上的理論是遠(yuǎn)遠(yuǎn)不夠的,只有在實(shí)戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用java+SSM+jsp+mysql+maven實(shí)現(xiàn)一個(gè)健身房管理系統(tǒng),大家可以在過程中查缺補(bǔ)漏,提升水平2021-11-11
Java使用POI導(dǎo)出大數(shù)據(jù)量Excel的方法
今天需要寫一個(gè)導(dǎo)出的Excel的功能,但是發(fā)現(xiàn)當(dāng)數(shù)據(jù)量到3萬條時(shí),列數(shù)在23列時(shí),內(nèi)存溢出,CPU使用100%,測試環(huán)境直接炸掉。小編給大家分享基于java使用POI導(dǎo)出大數(shù)據(jù)量Excel的方法,感興趣的朋友一起看看吧2019-11-11
java去除空格、標(biāo)點(diǎn)符號的方法實(shí)例
這篇文章主要給大家介紹了關(guān)于java去除空格、標(biāo)點(diǎn)符號的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
IDEA 2019.2.2配置Maven3.6.2打開Maven項(xiàng)目出現(xiàn) Unable to import Maven
這篇文章主要介紹了IDEA 2019.2.2配置Maven3.6.2打開Maven項(xiàng)目出現(xiàn) Unable to import Maven project的問題,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12
string boot 與 自定義interceptor的實(shí)例講解
下面小編就為大家分享一篇string boot 與 自定義interceptor的實(shí)例講解,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2017-12-12

