private List<ResConsumer> getResConsumers(String appName) {
List<AbilityEntity> consumers = getConsumerList(appName);
// 业务逻辑部分,省略....
Map<String, ResConsumer> result = new HashMap<>();
List<CompletableFuture<ResConsumer>> completableFutures = new ArrayList<>();
for (String dataTrace : dataTraces) {
CompletableFuture<ResConsumer> completableFuture = CompletableFuture.supplyAsync(() -> getConsumerAppInfo(dataTrace));
completableFutures.add(completableFuture);
}
//等待所有请求完毕
CompletableFuture<Void> resultFuture = CompletableFuture.allOf(completableFutures.toArray(new CompletableFuture[completableFutures.size()]));
// 阻塞线程等待
resultFuture.join();
// 遍历请求获取结果
for (CompletableFuture<ResConsumer> completableFuture : completableFutures) {
ResConsumer resConsumer = null;
try {
resConsumer = completableFuture.get();
if(resConsumer!=null) {
//添加到集合
if (result.get(resConsumer.getConsumerAppName()) == null) {
result.put(resConsumer.getConsumerAppName(), resConsumer);
} else {
result.get(resConsumer.getConsumerAppName()).getConsumerInterfaceNames().addAll(resConsumer.getConsumerInterfaceNames());
}
}
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
// 业务逻辑部分,省略....
}
completableFuture做下并发处理
最新推荐文章于 2024-07-05 13:50:10 发布