gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 00:10:39 26 4
gpt4 key购买 nike

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com