动态调整线程池核心参数

文章描述了一种使用Apollo配置管理框架的方法,当配置中的核心池大小(cpuCoreSize)和最大池大小(maxCoreSize)发生变化时,会自动调用线程池的修改方法,更新线程池的大小。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

项目

采用的是apollo配置,监听apollo配置是否修改,如果有变动,则调用线程池的修改参数方法

关键代码

ThreadPoolTaskExecutor.setCorePoolSize(cpuCoreSize);
ThreadPoolTaskExecutor.setMaxPoolSize(maxCoreSize);

public static final String CPU_CORE_SIZE = "cpuCoreSize";
public static final String MAX_CORE_SIZE = "maxCoreSize";

@ApolloConfigChangeListener("")
public void doThreadChangeHandler(ConfigChangeEvent changeEvent) throws Exception {
    if(changeEvent.isChanged(CPU_CORE_SIZE) || changeEvent.isChanged(MAX_CORE_SIZE)) {
        ThreadPoolTaskExecutor asyncPool = ThreadPoolUtil.getAsyncPool();
        log.info("doThreadChangeHandler start : old cpuCoreSize : {},old maxPoolSize : {}", asyncPool.getCorePoolSize(), asyncPool.getMaxPoolSize());
        int cpuCoreSize = Integer.valueOf(changeEvent.getChange(CPU_CORE_SIZE).getNewValue());
        int maxCoreSize = Integer.valueOf(changeEvent.getChange(MAX_CORE_SIZE).getNewValue());
        ThreadPoolUtil.getAsyncPool().setCorePoolSize(cpuCoreSize);
        ThreadPoolUtil.getAsyncPool().setMaxPoolSize(maxCoreSize);
        log.info("doThreadChangeHandler start : new cpuCoreSize : {},new maxPoolSize : {}", asyncPool.getCorePoolSize(), asyncPool.getMaxPoolSize());
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值