java - 在 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()
?
解决方案
只有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
推荐阅读
- iis - 随机获取登录提示
- xml - 序言中不允许内容,但序言在运行时可以吗?Groovy、Jenkins、Java、管道、XML
- java - 获取“无法从 TriggerBuilder 转换
在运行简单的 Quratz 教程程序时触发” - powerbi - DAX 计算所有单个产品的累计总和列(年初至今)
- redis - RedisBloom:添加项目(位字符串)的选项没有散列?
- c++ - C++ typelist 制作子列表
- c# - 如何将字符串转换为枚举数组(keycode[])
- java - 比较地图值并排序
- wordpress - 子域在我不知情的情况下进行 301 重定向
- java - NetBeans 11.0 使用来自 Maven 的源而不是其他开放项目