首页 > 解决方案 > 在 new CompletableFuture() 上注册的回调在哪个线程上执行?

问题描述

我是 Completable Futures 的新手,并试图了解在 CompletableFuture 上注册的回调在哪个线程上使用构造函数构造(new CompletableFuture())

例如:

CompletableFuture<String> future =
        CompletableFuture.supplyAsync(() -> {
            //...
        }, pool);
CompletableFuture<Integer> intFuture =
    future.thenApply(s -> s.length());

thenApply() 中的转换已注册,并将在任务完成后立即在与任务相同的线程中执行。

CompletableFuture<String> future = new CompletableFuture();
CompletableFuture<Integer> intFuture =
    future.thenApply(s -> s.length());
future.complete("hello");

thenApply() 中的转换已注册,一旦任务完成,它将在哪个线程上执行future.complete("hello")?它是在主线程上执行还是在主线程上执行ForkJoinPool.commonPool()

标签: javacompletable-future

解决方案


只有Async方法由新线程执行,因此在您的情况下thenApply由主线程执行

所有没有显式 Executor 参数的异步方法都使用 ForkJoinPool.commonPool() 执行(除非它不支持至少两个并行级别,在这种情况下,会创建一个新线程来运行每个任务)。

public class TestMain {

public static void main(String[] args) {

    CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
        System.out.println(Thread.currentThread().getName());
        return "hello";
    });
    CompletableFuture<Integer> intFuture = future.thenApply(s -> {
        System.out.println(Thread.currentThread().getName());
        return s.length();
    });

    CompletableFuture<Integer> intFuture2 = future.thenApply(s -> {
        System.out.println(Thread.currentThread().getName());
        return s.length();
        });
       future.complete("hello");

     }

 }

输出

ForkJoinPool.commonPool-worker-1
main
main

推荐阅读