private List<JSONObject> asyncGet(List<BO> list)
throws Exception {
List<JSONObject> objectList = new ArrayList<>();
CompletableFuture[] futures = list.stream()
.map(p -> CompletableFuture.supplyAsync(() -> get需要远程调用的接口(接口参数))
).toArray(CompletableFuture[]::new);
CompletableFuture<Void> completableFuture = CompletableFuture.allOf(futures);
completableFuture.get(3000, TimeUnit.MILLISECONDS);
for (CompletableFuture future:futures) {
objectList.add((JSONObject)future.get());
}
return objectList;
}
批量获取实现方式list
JAVA8 表达式CompletableFuture的异步批量获取使用
最新推荐文章于 2025-10-20 18:43:02 发布
本文介绍了一种使用CompletableFuture进行批量异步调用的方法,通过supplyAsync来并发执行远程接口请求,最后收集所有结果返回。
560

被折叠的 条评论
为什么被折叠?



