java 注解實現(xiàn)一個可配置線程池的方法示例
前言
項目需要多線程執(zhí)行一些Task,為了方便各個服務的使用。特意封裝了一個公共工具類,下面直接擼代碼:
PoolConfig(線程池核心配置參數(shù)):
/** * <h1>線程池核心配置(<b style="color:#CD0000">基本線程池數(shù)量、最大線程池數(shù)量、隊列初始容量、線程連接保持活動秒數(shù)(默認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> * 基本線程池數(shù)量 * </td></tr> * <tr><td> * count * </td><td> * 最大線程池數(shù)量 * </td></tr> * <tr><td> * maxCount * </td><td> * 隊列初始容量 * </td></tr> * <tr><td> * aliveSec * </td><td> * 線程連接保持活動秒數(shù)(默認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配置項以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">線程池核心配置、各個業(yè)務處理的任務數(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> * 線程池各個業(yè)務任務初始的任務數(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注解的任務集合:
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.因為使用的springboot項目,需要在application.properties 或者 application.yml 添加
#配置執(zhí)行的task線程數(shù) thread.count.NeedExcutorTask=4 #最大存活時間 thread.pool.aliveSec=300000 #其他配置同理
2.將我們寫的線程配置進行裝載到我們的項目中
@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("====== 任務執(zhí)行 =====") } }
以上就是創(chuàng)建一個可擴展的線程池相關的配置(望指教~~~)。希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Tk.mybatis零sql語句實現(xiàn)動態(tài)sql查詢的方法(4種)
有時候,查詢數(shù)據(jù)需要根據(jù)條件使用動態(tài)查詢,這時候需要使用動態(tài)sql,本文主要介紹了Tk.mybatis零sql語句實現(xiàn)動態(tài)sql查詢的方法,感興趣的可以了解一下2021-12-12Spring AOP結合注解實現(xiàn)接口層操作日志記錄
在項目開發(fā)中我們需要記錄接口的操作日志:包含請求參數(shù)、響應參數(shù)、接口所屬模塊、接口功能描述、請求地址、ip地址等信息;實現(xiàn)思路很簡單就是基于注解和aop的方式去記錄日志,主要的難點在于日志表結構、注解的設計已經(jīng)aop實現(xiàn)的一些比較好的實現(xiàn)方式的借鑒2022-08-08Java 實戰(zhàn)項目錘煉之嘟嘟健身房管理系統(tǒng)的實現(xiàn)流程
讀萬卷書不如行萬里路,只學書上的理論是遠遠不夠的,只有在實戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用java+SSM+jsp+mysql+maven實現(xiàn)一個健身房管理系統(tǒng),大家可以在過程中查缺補漏,提升水平2021-11-11Java使用POI導出大數(shù)據(jù)量Excel的方法
今天需要寫一個導出的Excel的功能,但是發(fā)現(xiàn)當數(shù)據(jù)量到3萬條時,列數(shù)在23列時,內(nèi)存溢出,CPU使用100%,測試環(huán)境直接炸掉。小編給大家分享基于java使用POI導出大數(shù)據(jù)量Excel的方法,感興趣的朋友一起看看吧2019-11-11IDEA 2019.2.2配置Maven3.6.2打開Maven項目出現(xiàn) Unable to import Maven
這篇文章主要介紹了IDEA 2019.2.2配置Maven3.6.2打開Maven項目出現(xiàn) Unable to import Maven project的問題,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-12-12string boot 與 自定義interceptor的實例講解
下面小編就為大家分享一篇string boot 與 自定義interceptor的實例講解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2017-12-12