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

private List asyncGet(List list)
    throws Exception {
    List objectList = new ArrayList<>();
    CompletableFuture[] futures = list.stream()
        .map(p -> CompletableFuture.supplyAsync(() -> get需要远程调用的接口(接口参数))
        ).toArray(CompletableFuture[]::new);
    CompletableFuture completableFuture = CompletableFuture.allOf(futures);
    completableFuture.get(3000, TimeUnit.MILLISECONDS);
    for (CompletableFuture future:futures) {
        objectList.add((JSONObject)future.get());
    }
    return objectList;
}
批量获取实现方式list

你可能感兴趣的:(JAVA8 表达式CompletableFuture的异步批量获取使用)