ForkJoinPool seems to waste a thread

前端 未结 2 1341
一向
一向 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条回答
  •  萌比男神i
    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.

提交回复
热议问题