从java1.7开始,引入了parallelStream的方式使用ForkJoinPool多线程处理数据的方式,ForkJoinPool默认线程池大小是cpu内核数-1,并且可以通过以下方式配置线程池大小:
System.setProperty("java.util.concurrent.ForkJoinPool.common.parallelism", PARALLE_COUNT);
但是上述配置,在springboot2.4.0下面可以生效,在springboot2.6.5下面不生效 ,通过查找原因,发现上述配置必须得在ForkJoinPool在注入之前才能生效,所以进行修改如下:
package com.copotematch.tools.datahandler.config;import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;@Configuration
public class ForkJoinPoolConfig implements BeanPostProcessor {@AutowiredEnvironment environment;@Overridepublic Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {String property = environment.getProperty("copote.match.paralles.count");if (property == null || "".equals(property)) {property = "10";}System.setProperty("java.util.concurrent.ForkJoinPool.common.parallelism", property);return bean;}}
或者新增jvm参数也可以:
-Djava.util.concurrent.ForkJoinPool.common.parallelism=10