Search code examples
javathreadpoolthreadpoolexecutor

What is the thread rejection policy of ForkJoinPool.commonPool()?


I have parallelStreams() in my code, and it is using the ForkJoinPool.

A thread pool executor has 4 predefined handler policies, and I'd like to know which one it is used as default in the common pool (if any of these). I can't find it in the documentation.

In the default ThreadPoolExecutor.AbortPolicy, the handler throws a runtime RejectedExecutionException upon rejection.

In ThreadPoolExecutor.CallerRunsPolicy, the thread that invokes execute itself runs the task. This provides a simple feedback control mechanism that will slow down the rate that new tasks are submitted.

In ThreadPoolExecutor.DiscardPolicy, a task that cannot be executed is simply dropped.

In ThreadPoolExecutor.DiscardOldestPolicy, if the executor is not shut down, the task at the head of the work queue is dropped, and then execution is retried (which can fail again, causing this to be repeated.)

From Oracle's documentation.


Solution

  • From ForkJoinPool's javadoc,

    This implementation rejects submitted tasks (that is, by throwing RejectedExecutionException) only when the pool is shut down or internal resources have been exhausted.

    This is the same behavior as ThreadPoolExecutor.AbortPolicy.