[concurrency-interest] ForkJoinPool not designed for nested Java 8 streams.parallel().forEach( ... )

Christian Fries email at christian-fries.de
Thu May 8 04:10:56 EDT 2014


Dear Doug.

Thank you for your reply. With respect to...

> This does not in general do anything interesting beyond
> making the task recursion stack shallower so that it becomes
> less likely that nested joiners need compensation.

...I believe that the performance issue I am seeing is hard to be attributed to „less likely that nested joiners need compensation“ (90 sec vs. 30 sec - I can provide you with more benchmarks on top of what is already on SO).

In addition I do not understand how this can be an explanation for the following behavior: The following code DEADLOCKS

	// Outer loop
	IntStream.range(0,numberOfTasksInOuterLoop).parallel().forEach(i -> {
		// do work here
		synchronized(this) {
			// Inner loop
			IntStream.range(0,numberOfTasksInInnerLoop).parallel().forEach(j -> {
				// do work here
			});
		}
	});

but the following workaround does no longer deadlock

	// Outer loop
	IntStream.range(0,numberOfTasksInOuterLoop).parallel().forEach(i -> {
		// do work here
		synchronized(this) {
			wrapInThread(() ->
				// Inner loop
				IntStream.range(0,numberOfTasksInInnerLoop).parallel().forEach(j -> {
					// do work here
				})
			);
		}
	});

here wrapInThread is just a simple method running its Runnable argument in a new Thread, immediately joining it with the caller.

A complete demo (just execute its main()) can be found here: http://svn.finmath.net/finmath%20experiments/trunk/src/net/finmath/experiments/concurrency/NestedParallelForEachAndSynchronization.java

The associated SO post is here: http://stackoverflow.com/questions/23489993/nested-java-8-parallel-foreach-loop-perform-poor-is-this-behavior-expected 

Best
Christian


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://cs.oswego.edu/pipermail/concurrency-interest/attachments/20140508/375b134f/attachment-0001.html>


More information about the Concurrency-interest mailing list