Class ForkJoinUtils


  • public final class ForkJoinUtils
    extends Object
    Utility class to support the use of {@link ForkJoinPool|ForkJoinPools} and related APIs.
    • Method Detail

      • submitInCustomPool

        public static ForkJoinTask<Void> submitInCustomPool​(String poolName,
                                                            int parallelism,
                                                            Runnable task)
        Runs a task in a custom ForkJoinPool. This is useful when a custom pool is needed for the execution of a task that forks {@link java.util.concurrent.ForkJoinTask|ForkJoinTasks} internally.

        In contrast to executeInCustomPool(String, int, Runnable) this method returns a ForkJoinTask, and there is no guarantee that the execution has completed until ForkJoinTask.join() (or a similar method) is called.

        Using a custom ForkJoinPool can be useful if I/O heavy loads are being processed, as it is ill-advised to use the common pool for such tasks. The common pool is optimized for CPU-bound tasks and running I/O heavy loads in it can lead to thread starvation.

        The terminal operation of a parallel stream is such an example:

             ForkJoinTask task = ForkJoinUtils.submitInCustomPool("myPool", 4, () -> Stream.of(files)
                 .parallel()
                 .forEach(file -> file.delete());
             task.join();
         
        Parameters:
        poolName - the name of the custom thread pool; thread names are prefixed with this name
        parallelism - the number of threads in the pool
        task - the task to run
        Returns:
        the result of the task execution
        See Also:
        submitInCustomPool(String, int, Callable), executeInCustomPool(String, int, Runnable)
      • executeInCustomPool

        public static void executeInCustomPool​(String poolName,
                                               int parallelism,
                                               Runnable task)
        Runs a task in a custom ForkJoinPool. This is useful when a custom pool is needed for the execution of a task that forks {@link java.util.concurrent.ForkJoinTask|ForkJoinTasks} internally.

        In contrast to submitInCustomPool(String, int, Runnable) this method blocks until all threads have completed their execution.

        Using a custom ForkJoinPool can be useful if I/O heavy loads are being processed, as it is ill-advised to use the common pool for such tasks. The common pool is optimized for CPU-bound tasks and running I/O heavy loads in it can lead to thread starvation.

        The terminal operation of a parallel stream is such an example:

             ForkJoinUtils.executeInCustomPool("myPool", 4, () -> Stream.of(files)
                 .parallel()
                 .forEach(file -> file.delete());
         
        Parameters:
        poolName - the name of the custom thread pool; thread names are prefixed with this name
        parallelism - the number of threads in the pool
        task - the task to run
        See Also:
        submitInCustomPool(String, int, Runnable)
      • submitInCustomPool

        public static <T> ForkJoinTask<T> submitInCustomPool​(String poolName,
                                                             int parallelism,
                                                             Callable<T> task)
        Runs a task in a custom ForkJoinPool. This is useful when a custom pool is needed for the execution of a task that forks {@link java.util.concurrent.ForkJoinTask|ForkJoinTasks} internally.

        In contrast to invokeInCustomPool(String, int, Callable) this method returns a ForkJoinTask, and there is no guarantee that the execution has completed until ForkJoinTask.join() (or a similar method) is called.

        Using a custom ForkJoinPool can be useful if I/O heavy loads are being processed, as it is ill-advised to use the common pool for such tasks. The common pool is optimized for CPU-bound tasks and running I/O heavy loads in it can lead to thread starvation.

        The terminal operation of a parallel stream is such an example:

             List bytes = ForkJoinUtils.executeInCustomPool("myPool", 4, () -> Stream.of(files)
                 .parallel()
                 .map(file -> file.readBytes())
                 .collect(Collectors.toList()));
         
        Parameters:
        poolName - the name of the custom thread pool; thread names are prefixed with this name
        parallelism - the number of threads in the pool
        task - the task to run
        Returns:
        the result of the task execution
        See Also:
        invokeInCustomPool(String, int, Callable)
      • invokeInCustomPool

        public static <T> T invokeInCustomPool​(String poolName,
                                               int parallelism,
                                               Callable<T> task)
        Runs a task in a custom ForkJoinPool. This is useful when a custom pool is needed for the execution of a task that forks {@link java.util.concurrent.ForkJoinTask|ForkJoinTasks} internally.

        In contrast to submitInCustomPool(String, int, Callable) this method blocks until all threads have completed their execution and returns the result of the task.

        Using a custom ForkJoinPool can be useful if I/O heavy loads are being processed, as it is ill-advised to use the common pool for such tasks. The common pool is optimized for CPU-bound tasks and running I/O heavy loads in it can lead to thread starvation.

        The terminal operation of a parallel stream is such an example:

             List bytes = ForkJoinUtils.executeInCustomPool("myPool", 4, () -> Stream.of(files)
                 .parallel()
                 .map(file -> file.readBytes())
                 .collect(Collectors.toList()));
         
        Parameters:
        poolName - the name of the custom thread pool; thread names are prefixed with this name
        parallelism - the number of threads in the pool
        task - the task to run
        Returns:
        the result of the task execution
        See Also:
        invokeInCustomPool(String, int, Callable)