代码很简单就,为了提升效率,使用线程并行处理大块的数据 ,项目运行后,一开始不会报错(futures.add(CompletableFuture.supplyAsync(()这行报错),运行了一段时间后就会出现图中报错,找不到什么原因,很少用CompletableFuture,之前都没发生这样的问题,从现象来看像是累计内存的占用未释放的感觉,一开始我是数量20, 线程数20, 后来改成,100和线程数10,依然在运行了一段时间后报错。。。有没有懂得大神帮帮忙看一下
List<HOProdCenterInfoVOData> getProductsInfo = new ArrayList<>();
List<List<String>> itemNoListList = new ArrayList<>();
if(itemNoList != null) {
itemNoListList = Lists.partition(itemNoList, 20);//每次数量
List<CompletableFuture<List<HOProdCenterInfoVOData>>> futures = new ArrayList<>();
ExecutorService threadPool = Executors.newScheduledThreadPool (20);//线程数
itemNoListList.stream().forEach(oneTimeItemNoList -> {
futures.add(CompletableFuture.supplyAsync(() -> {
if(oneTimeItemNoList.size() > 0) {
return HOProdCenterClient.getProductsInfo(storeNo, oneTimeItemNoList, 0, 1000);
}
return null;
},threadPool).handle((res, e) -> {
if(e != null || res == null) {
return new ArrayList<HOProdCenterInfoVOData>();
}
return res;
}));
});
CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).join();
for (CompletableFuture<List<HOProdCenterInfoVOData>> future : futures) {
try {
List<HOProdCenterInfoVOData> oneTimeProductsInfo= future.get();
getProductsInfo.addAll(oneTimeProductsInfo);
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
continue;
}
}
}
P.S.2022.12.05补充:
不好意思漏说一些基本信息,刚开始服务器是8核8G的jvm内存4G,后来因为这个原因还升级内存到16G,jvm内存8G,依然还是报这个错。。后来从查了一下,阿里是禁止使用Executors来创建线程池的,使用Executors创建就会又OOM的风险,需要使用ThreadPoolExecutor来自定义创建线程池,所以线程池的创建我改成了下面这样,但是依然发生了同样的报错
ExecutorService pool = new ThreadPoolExecutor(17,80,1, TimeUnit.SECONDS,new ArrayBlockingQueue<Runnable>(80),
Executors.defaultThreadFactory(),
new ThreadPoolExecutor.DiscardOldestPolicy());
还有一个重要的点, 可能是这个引起,这个段代码并不是JOB中的,而是页面显示的代码,因为一个页面某些需求的缘故是不分页的,虽然数量最大页也就几千条,但中间需要调用很多接口就造成了显示缓慢,所以我才想到加入线程处理。
看报错应该是无法创建新的本地线程了,addWorker()时报错了。
是什么导致无法创建线程?

我认为是cpu没有资源了,根本原因如下:
线程池居然声明在方法体内

可以看到他的核心线程数被设置了。
一般情况下,不会回收核心线程,因为线程池本身就是实现线程的复用
建议:
ExecutorService threadPool = Executors.newScheduledThreadPool (20);
这个不要放在方法体内,而是声明为类的属性或者单独维护