java - 为什么 CompletableFuture.runAsync() 并不总是提交给 ForkJoinPool.commonPool()?

标签 java multithreading asynchronous completable-future forkjoinpool

CompletableFuture.runAsync 文档说明:

Returns a new CompletableFuture that is asynchronously completed by a task running in the ForkJoinPool.commonPool() after it runs the given action.

但是,据我所知,runAsync只向ForkJoinPool.commonPool()提交任务当ForkJoinPool.getCommonPoolParallelism() > 1 。如果没有,它会手动创建一个新的 Thread对于每个提交的任务。

为什么会出现这种情况?

最佳答案

是的,如果 ForkJoinPool 并行度小于 2,则为每个任务创建新线程 here

All async methods without an explicit Executor argument are performed using the ForkJoinPool.commonPool() (unless it does not support a parallelism level of at least two, in which case, a new Thread is created to run each task).

关于java - 为什么 CompletableFuture.runAsync() 并不总是提交给 ForkJoinPool.commonPool()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59426187/

相关文章:

c# - 编码任务在特定线程上运行

java - 获取 "Set-Cookie" header

java - 内存直接映射到字符串

c - 防止可能的数据竞争情况

java - Java中的多个线程导致问题

hadoop - “Apache Oozie actions are asynchronous”的意思?

java - 如何将 @RequestParam Map<String, String> 转换为自定义 Map<>

java - 如果在java中调整表单大小,如何使组件粘在角落而不是保持在同一位置?

python - PEP 0492 - Python 3.5 异步关键字

java - 从 Servlet 启动独立应用程序的线程