【发布时间】:2020-05-09 18:52:12
【问题描述】:
下面的代码 sn-p 工作得很好。使用所有线程
片段 1
scrolledPage.stream()
.filter(this::isUserDoesntHaveId)
.map(this::processSingle) // processSingle method return supplier
.map(task -> CompletableFuture.supplyAsync(task, executorService)) // map to CompletableFuture
.collect(Collectors.toList()) // Collect those as list
.stream() // then again making another stream out of that.
.map(CompletableFuture::join)
.filter(Objects::nonNull)
.collect(Collectors.toList());
我对这段代码感到困惑
.map(task -> CompletableFuture.supplyAsync(task, executorService)) // map to CompletableFuture
.collect(Collectors.toList()) // Collect those as list
.stream() // then again making another stream out of that.
.map(CompletableFuture::join)
为什么我需要收集然后再次制作它。我已经测试过如下。
片段 2
scrolledPage.stream()
.filter(this::isUserDoesntHaveId)
.map(this::processSingle) // processSingle method return supplier
.map(task -> CompletableFuture.supplyAsync(task, executorService)) // map to CompletableFuture
.map(CompletableFuture::join) // this code is similar without the collecting part.
.filter(Objects::nonNull)
.collect(Collectors.toList());
使用我看到的最后一个代码 sn-p 只使用线程池中的一个线程。但是第一个使用每个线程。这两个代码sn-ps有什么区别。
【问题讨论】:
标签: java java-8 completable-future