简体   繁体   English

Java8 thenCompose和thenComposeAsync之间的区别

[英]Difference between Java8 thenCompose and thenComposeAsync

Given this piece of code: 给出这段代码:

public List<String> findPrices(String product){
    List<CompletableFuture<String>> priceFutures =
    shops.stream()
         .map(shop -> CompletableFuture.supplyAsync(
                () -> shop.getPrice(product), executor))
         .map(future -> future.thenApply(Quote::parse))
         .map(future -> future.thenCompose(quote ->
                CompletableFuture.supplyAsync(
                        () -> Discount.applyDiscount(quote), executor
                )))
         .collect(toList());

    return priceFutures.stream()
                       .map(CompletableFuture::join)
                       .collect(toList());
}

This part of it: 这部分内容:

.map(future -> future.thenCompose(quote ->
                CompletableFuture.supplyAsync(
                        () -> Discount.applyDiscount(quote), executor
                )))

Could it be rewrite as: 可以改写为:

.map(future -> 
    future.thenComposeAsync(quote -> Discount.applyDiscount(quote), executor))

I took this code from an example of a book and says the two solutions are different, but I do not understand why. 我从一本书的示例中获取了这段代码,并说这两种解决方案是不同的,但是我不明白为什么。

Let's consider a function that looks like this: 让我们考虑一个看起来像这样的函数:

public CompletableFuture<String> requestData(String parameters) {
    Request request = generateRequest(parameters);
    return CompletableFuture.supplyAsync(() -> sendRequest(request));
}

The difference will be with respect to which thread generateRequest() gets called on. 区别在于调用哪个线程generateRequest()

thenCompose will call generateRequest() on the same thread as the upstream task (or the calling thread if the upstream task has already completed). thenCompose将在与上游任务相同的线程上调用generateRequest() (如果上游任务已经完成,则调用线程)。

thenComposeAsync will call generateRequest() on the provided executor if provided, or on the default ForkJoinPool otherwise. thenComposeAsync将在提供的执行程序(如果提供generateRequest()上调用generateRequest() ,否则在默认的ForkJoinPool上调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 作者使用 thenCompose 而不是 thenComposeAsync 的理由是否正确 - Is the writer's reason correct for using thenCompose and not thenComposeAsync Spring Async、Async with CompletableFuture 和 Java8 并行 stream 之间的区别 - Difference between Spring Async, Async with CompletableFuture and Java8 parallel stream Java8 中的谓词和函数接口有什么区别? - What is the difference between a Predicate and a Function Interface in Java8? java8 中“使用 Classname.staticMethod 调用 static 方法”和“使用 Classname::staticMethod 调用”之间有什么区别吗 - Is there any difference between “invoking a static method with Classname.staticMethod” & “invoking with Classname::staticMethod” in java8 Joda Time和Java8时差 - Joda Time and Java8 Time difference 在Java8和Java7环境之间切换 - Changing between Java8 and Java7 Environments Java7和Java8之间的SSL不兼容? - SSL Incompatabilities between Java7 and Java8? 如何在 Java 8 中多次使用 thenCompose 的结果? - How to use the result of a thenCompose multiple times in Java 8? Java-在异步thenCompose内部进行同步调用 - Java - sync call inside async thenCompose Java CompletableFuture thenCompose 与几个异步任务 - Java CompletableFuture thenCompose with several async tasks
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM