配置 Fork Join 线程池

Configuring the Fork Join Thread Pool

我有一个关于使用 fork join 线程池的简单问题。这是我正在使用的一个简短示例:

  executor = "fork-join-executor"
  # Configuration for the fork join pool
  fork-join-executor {
    # Min number of threads to cap factor-based parallelism number to
    parallelism-min = 24
    # Parallelism (threads) ... ceil(available processors * factor)
    parallelism-factor = 4.0
    # Max number of threads to cap factor-based parallelism number to
    parallelism-max = 48
  }

我不确定的是,在这种情况下将创建多少个线程?我 运行 宁在 2 核机器上,所以它是每个核心 24 个线程,最多 48 个线程?

将并行系数设置为 4.0,可以 运行 并行的线程数将是 8。那么需要设置最小值和最大值,我的情况是24 和 48?

让我们首先解释 ForkJoinPool works. The number of threads is controlled by a single (constructor) parameter parallelism (the number of cores by default). ForkJoinPool tries to keep at most parallelism running threads. But it can decide to spawn more threads, if some of them are blocked. E.g. If you have too many forked tasks that are waiting for join, your computation would be blocked unless you add new threads; the waiting threads are active but not running (because they wait). More explanation here 中的并行性。

您问题中的调度程序配置控制 parallelism 参数的值。它设置为 可用处理器 * parallelism-factor,但至少 parallelism-min,最多 parallelism-max。查看更多信息 here and here

How many threads will be created in this case?

并行度应该是2 cores * 4 -> 8 threads。所以运行个线程应该是8个,但是总共创建的线程数可能会更高

What is the need for setting the min and max values which is my case is 24 and 48

我认为只有在您部署在具有不同内核数的机器上时才会出现这种情况。