JAVA8 表达式CompletableFuture的异步批量获取使用

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
    原文作者:icool_ali
    原文地址: https://blog.csdn.net/icool_ali/article/details/80910310
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞