Search code examples
javaconcurrencyfork-joinforkjoinpool

RecursiveAction thread pool size


I just saw a use for the Java 7 RecursiveAction.invokeAll(), and I wonder:

What is the size of the threadpool of tasks? (or the algorithm which determines it..)

If I have a lot of tasks (say 100 tasks), should I use it?


Solution

  • The size of the thread pool is whatever you set it to be, either the number of cores that you have:

    public ForkJoinPool()

    Creates a ForkJoinPool with parallelism equal to Runtime.availableProcessors(), using the default thread factory, no UncaughtExceptionHandler, and non-async LIFO processing mode.

    Or some specific number:

    public ForkJoinPool(int parallelism)

    Creates a ForkJoinPool with the indicated parallelism level, the default thread factory, no UncaughtExceptionHandler, and non-async LIFO processing mode.

    Please read the JavaDoc for more information.

    The way a recursive action works is that it spawns multiple versions of itself. You always start with 1. The number of actions generated is somewhat immaterial, hundreds or thousands should be fine - as long as they don't "pile up".