Class ForkJoinUtils
- java.lang.Object
-
- org.apache.jackrabbit.oak.commons.internal.concurrent.ForkJoinUtils
-
public final class ForkJoinUtils extends Object
Utility class to support the use of {@link ForkJoinPool|ForkJoinPools} and related APIs.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static voidexecuteInCustomPool(String poolName, int parallelism, Runnable task)Runs a task in a customForkJoinPool.static <T> TinvokeInCustomPool(String poolName, int parallelism, Callable<T> task)Runs a task in a customForkJoinPool.static ForkJoinTask<Void>submitInCustomPool(String poolName, int parallelism, Runnable task)Runs a task in a customForkJoinPool.static <T> ForkJoinTask<T>submitInCustomPool(String poolName, int parallelism, Callable<T> task)Runs a task in a customForkJoinPool.
-
-
-
Method Detail
-
submitInCustomPool
public static ForkJoinTask<Void> submitInCustomPool(String poolName, int parallelism, Runnable task)
Runs a task in a customForkJoinPool. 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 aForkJoinTask, and there is no guarantee that the execution has completed untilForkJoinTask.join()(or a similar method) is called.Using a custom
ForkJoinPoolcan 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:
ForkJoinTasktask = 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 nameparallelism- the number of threads in the pooltask- 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 customForkJoinPool. 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
ForkJoinPoolcan 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 nameparallelism- the number of threads in the pooltask- 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 customForkJoinPool. 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 aForkJoinTask, and there is no guarantee that the execution has completed untilForkJoinTask.join()(or a similar method) is called.Using a custom
ForkJoinPoolcan 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:
Listbytes = 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 nameparallelism- the number of threads in the pooltask- 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 customForkJoinPool. 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
ForkJoinPoolcan 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:
Listbytes = 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 nameparallelism- the number of threads in the pooltask- the task to run- Returns:
- the result of the task execution
- See Also:
invokeInCustomPool(String, int, Callable)
-
-