CompletableFuture简单使用

开四个线程,异步+并行地从远端获取结果,然后解析,放入集合

List<Integer> priceList = new ArrayList(4);

CompletableFuture.supplyAsync(() -> this.getPriceFromRemote()).thenAccept(result -> priceList.add(this.parsePrice(result)));
CompletableFuture.supplyAsync(() -> this.getPriceFromRemote()).thenAccept(result -> priceList.add(this.parsePrice(result)));
CompletableFuture.supplyAsync(() -> this.getPriceFromRemote()).thenAccept(result -> priceList.add(this.parsePrice(result)));
CompletableFuture.supplyAsync(() -> this.getPriceFromRemote()).thenAccept(result -> priceList.add(this.parsePrice(result)));
posted @ 2021-01-11 22:16  JaxYoun  阅读(116)  评论(0编辑  收藏  举报