问题描述
请在下面找到我的实际代码的虚构示例。这个例子过于简单,无法解释我想要实现的目标。
public class TestClass {
ForkJoinPool forkJoinPool = new ForkJoinPool(3);
@Test
public void testSample(){
List<String> testStrings = Arrays.asList("Hello","World","Cobra","Eagle","Sam");
//This doesn't compile
List<CompletableFuture<Double>> result =
testStrings.stream().map(each -> CompletableFuture.supplyAsync(() -> getIndividualCharacters(each),forkJoinPool)
.thenComposeAsync(listofChars -> listofChars.stream()
.map(character -> CompletableFuture.supplyAsync(() -> getDoubleString(character)))
.collect(Collectors.toList())));
}
public List<String> getIndividualCharacters(String name){
List<String> result = new ArrayList<>();
for(int i =0; i < name.length(); i++){
result.add(Character.toString(name.charat(i)));
}
return result;
}
public Double getDoubleString(String singleCharacter){
return Math.random();
}
}
我的 getIndividualCharacters
方法返回结果列表(异步)。我使用单个结果并进一步处理它以返回另一个结果(异步)。
我想要的最终结果是 List<Completeable<final result>>
在这种情况下 List<Completeable<Double>>
我可以在里面使用
CompleteablFuture.allOf
如果可能,我想使用 CompleteableFuture 的链接。我还没有设法找到一种方法来做到这一点,也没有任何例子提到它。 关于如何实现这一目标的任何帮助或指示都会非常有帮助。
PS:我已经设法使用两个单独的 CompleteableFuture 流解决了这个问题,但是,我想使用链接 thenCompose
List<String> testStrings = Arrays.asList("Hello","Sam");
List<CompletableFuture<List<String>>> firstResult = testStrings.stream()
.map(each -> CompletableFuture.supplyAsync(() -> getIndividualCharacters(each),forkJoinPool))
.collect(Collectors.toList());
CompletableFuture.allOf(firstResult.toArray(new CompletableFuture[firstResult.size()])).join();
List<CompletableFuture<Double>> secondResult = firstResult.stream()
.flatMap(res -> res.join().stream())
.map(ea -> CompletableFuture.supplyAsync(() -> getDoubleString(ea),forkJoinPool))
.collect(Collectors.toList());
List<Double> finalResult = secondResult.stream().map(res-> res.join()).collect(Collectors.toList());
System.out.println("finalResult " + finalResult);
问候。
解决方法
我猜是这样的吗?
List<CompletableFuture<Double>> result =
testStrings.stream()
.map(x -> CompletableFuture.supplyAsync(() -> getIndividualCharacters(x)))
.map(x -> x.thenApplyAsync(y -> y.stream().map(z -> CompletableFuture.supplyAsync(() -> getDoubleString(z)))))
.flatMap(CompletableFuture::join)
.collect(toList());
请注意,这会因为 join
而阻塞。
我会添加另一个答案,也许这可以为您提供解决方案。您可以将 join
的执行从上一个答案延迟到您需要它为止。例如通过:
Stream<CompletableFuture<Double>> result =
testStrings.stream()
.map(each -> supplyAsync(() -> getIndividualCharacters(each)))
.map(x -> x.thenCompose(y -> supplyAsync(() -> y.stream().map(z -> supplyAsync(() -> getDoubleString(z))))))
.flatMap(x -> Stream.of(x).map(CompletableFuture::join))
.flatMap(Function.identity());
但这同时会将结果更改为 Stream
- 只有在调用终端操作时才会评估流。