动态调整线程池核心参数

项目

采用的是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());
    }

}

猜你喜欢

转载自blog.csdn.net/qq_44961149/article/details/132448818
今日推荐