Difference between Java8 thenCompose and thenComposeAsync

前端 未结 1 1832
深忆病人
深忆病人 2021-01-01 01:52

Given this piece of code:

public List findPrices(String product){
    List> priceFutures =
    shops.stre         


        
1条回答
  •  隐瞒了意图╮
    2021-01-01 02:39

    Let's consider a function that looks like this:

    public CompletableFuture 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.

    thenCompose will call generateRequest() on the same thread as the upstream task (or the calling thread if the upstream task has already completed).

    thenComposeAsync will call generateRequest() on the provided executor if provided, or on the default ForkJoinPool otherwise.

    0 讨论(0)
提交回复
热议问题