知道我的 RecursiveTask 将在哪个 ForkJoinPool 上执行

Knowing on which ForkJoinPool my RecursiveTask is going to be executed on

我很难理解如何确保我 运行 在我自己的 ForkjoinPool 中执行我的分叉任务,而不是让它使用我假设的公共池当您没有指明要在哪个线程池中时会发生什么 运行.

查看下面的代码,我的 fjp 实例和新实例化的 FibonacciCalculator 对象之间似乎没有 link。这让我假设我的 fjp 实际上没有被用于子任务。怎么做?

object FJPoolApp extends App {
  val fjp = new ForkJoinPool()
  println(fjp.submit(new FibonacciCalculator(10)).get)
}

class FibonacciCalculator(k : Int) extends RecursiveTask[Int] {
  override def compute(): Int = {
    if (k <= 1) k
    else {
      val left = new FibonacciCalculator(k-1).fork()  <-- where is this being run?
      val right = new FibonacciCalculator(k-2).fork() <-- where is this being run?
      left.join() + right.join()
    }
  }
}

ForkJoinTask#fork() 的文档是这样说的:

Arranges to asynchronously execute this task in the pool the current task is running in [emphasis added], if applicable, or using the ForkJoinPool.commonPool() if not inForkJoinPool().

这里是 ForkJoinTask#inForkJoinPool() 的文档:

Returns true if the current thread is a ForkJoinWorkerThread executing as a ForkJoinPool computation

由于您在 ForkJoinPool 中执行原始 FibonacciCalculator,因此在任务中创建的分支也在该 相同 池中执行。