I would like to globally replace the common thread pool used by default by the Java parallel streams, i.e., for example for
IntStream.range(0,100).parallel().forEach(i -> {
doWork();
});
I know that it is possible to use a dedicated ForkJoinPool by submitting such instruction to a dedicated thread pool (see Custom thread pool in Java 8 parallel stream ). The question here is
- Is it possible to replace the common ForkJoinPool by some other implementation (say a
Executors.newFixedThreadPool(10)
? - Is it possible to do so by some global setting, e.g., some JVM property?
Remark: The reason why I like to replace the F/J pool is, because it appears to have a bug which makes it unusable for nested parallel loops.
Nested parallel loops have poor performance and may lead to deadlocks, see http://christian-fries.de/blog/files/2014-nested-java-8-parallel-foreach.html
For example: The following code leads to a deadlock:
// Outer loop
IntStream.range(0,24).parallel().forEach(i -> {
// (omitted:) do some heavy work here (consuming majority of time)
// Need to synchronize for a small "subtask" (e.g. updating a result)
synchronized(this) {
// Inner loop (does s.th. completely free of side-effects, i.e. expected to work)
IntStream.range(0,100).parallel().forEach(j -> {
// do work here
});
}
});
(even without any additional code at "do work here", given that parallelism is set to < 12).
My question is how to replace the FJP. If you like to discuss nested parallel loops, you might check Nested Java 8 parallel forEach loop perform poor. Is this behavior expected? .
I think that's not the way the stream API is intended to be used. It seems you're (mis)using it for simply doing parallel task execution (focusing on the task, not the data), instead of doing parallel stream processing (focusing on the data in the stream). Your code somehow violates some of the main principles for streams. (I'm writing 'somehow' as it is not really forbidden but discouraged): Avoid states and side effects.
Apart from that (or maybe because of side effects), you're using heavy synchronization within your outer loop, which is everything else but harmless!
Although not mentioned in the documentation, parallel streams use the common
ForkJoinPool
internally. No matter whether or not this is a lack of documentation, we must simply accept that fact. The JavaDoc ofForkJoinTask
states:Again, it seems that you're using streams as replacement for a simple for-loop and an executor service.
n
tasks in parallel, use anExecutionService
ForkJoinPool
(withForkJoinTasks
) instead. (It ensures a constant number of threads without the danger of a deadlock because of too many tasks waiting for others to complete, as waiting tasks do not block their executing threads).Don't mix up
ExecutionService
andForkJoinPool
. They are (usually) not a replacement for each other!Although your original question is already answered well by isnot2bad, it might be important for you that the described bug (the reason for your wish to exchange the FJP implementation) seems to be fixed with java 1.8.0.40. See Nested Java 8 parallel forEach loop perform poor. Is this behavior expected?