Difference between Java8 thenCompose and thenComposeAsync

Cil*_*lla 8 java java-8 completable-future

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());
}
Run Code Online (Sandbox Code Playgroud)

This part of it:

.map(future -> future.thenCompose(quote ->
                CompletableFuture.supplyAsync(
                        () -> Discount.applyDiscount(quote), executor
                )))
Run Code Online (Sandbox Code Playgroud)

Could it be rewrite as:

.map(future -> 
    future.thenComposeAsync(quote -> Discount.applyDiscount(quote), executor))
Run Code Online (Sandbox Code Playgroud)

I took this code from an example of a book and says the two solutions are different, but I do not understand why.

Joe*_*e C 10

Let's consider a function that looks like this:

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

The difference will be with respect to which thread generateRequest() gets called on.

thenCompose将调用generateRequest()与上游任务相同的线程(如果上游任务已经完成,则调用调用线程)。

thenComposeAsyncgenerateRequest()如果提供,将调用提供的执行程序,ForkJoinPool否则调用默认执行程序。