ForkJoinPool seems to waste a thread

前端 未结 2 1328
一向
一向 2020-12-10 16:52

I\'m comparing two variations on a test program. Both are operating with a 4-thread ForkJoinPool on a machine with four cores.

In \'mode 1\', I use the pool very muc

相关标签:
2条回答
  • 2020-12-10 17:38

    The classic use of invokeAll for a Fork Join pool is to fork one task and compute another (in that executing thread). The thread that does not fork will join after it computes. The work stealing comes in with both tasks computing. When each task computes it is expected to fork it's own subtasks (until some threshold is met).

    I am not sure what invokeAll is being called for your RecursiveAction.compute() but if it is the invokeAll which takes two RecursiveAction it will fork one, compute the other and wait for the forked task to finish.

    This is different then a plain executor service because each task of an ExecutorService is simply a Runnable on a queue. There is no need for two tasks of an ExecutorService to know the outcome of another. That is the primary use case of a FJ Pool.

    0 讨论(0)
  • 2020-12-10 17:54

    ForkJoinTask.invokeAll is forking all tasks, but the first in the list. The first task it runs itself. Then it joins other tasks. It's thread is not released in any way to the pool. So you what you see, it it's thread blocking on other tasks to be complete.

    0 讨论(0)
提交回复
热议问题