How to use CompletableFuture.thenComposeAsync()?

前端 未结 2 2024
孤独总比滥情好
孤独总比滥情好 2021-01-12 01:08

Given:

public class Test
{
    public static void main(String[] args)
    {
        int nThreads = 1;
        Executor e = Executors.newFixedThreadPool(nThre         


        
2条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-12 01:30

    It blocks on runAsync that's inside thenComposeAsync. thenComposeAsync runs the supplied function in a thread inside executor e. But the function you gave it tries itself to execute the body of runAsync inside the same executor.

    You can see better what's going on by adding another trace output:

    CompletableFuture.runAsync(() -> {
        System.out.println("Task 1. Thread: " + Thread.currentThread().getId());
    }, e).thenComposeAsync((Void unused) -> {
        System.out.println("Task 1 1/2. Thread: " + Thread.currentThread().getId());
        return CompletableFuture.runAsync(() -> {
            System.out.println("Task 2. Thread: " + Thread.currentThread().getId());
        }, e);
    }, e).join();
    

    Now if you run it with a 2-thread executor, you will see that Task 1 1/2 and Task 2 run on different threads.

    The way to fix it is to replace thenComposeAsync with just regular thenCompose. I am not sure why you would ever use thenComposeAsync. If you have a method that returns a CompletableFuture, presumably that method doesn't block and doesn't need to be run asynchronously.

提交回复
热议问题