The following code works with JDK16 reproducibly and hangs with JDK17 reproducibly on my laptop (4/8-core) with basic command line options: "-ea". A JDK-ticket exists (https://bugs.openjdk.org/browse/JDK-8281524), but there was disagreement whether the usage is "good" or not. Then, radio silence in the last 6 months. Can anybody help pinpoint my usage error (if any) and how to fix it?
import java.util.Arrays;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinTask;
import java.util.stream.Stream;
import org.junit.Test;
public class FjpTest {
@Test
public void testFjp() {
final ForkJoinPool fjPool = new ForkJoinPool(1);
final String[] objs = Stream.generate(() -> "").limit(10_000).toArray(String[]::new);
// the following line should sort the array,
// but instead causes a single-threaded spin-wait
fjPool.invoke(ForkJoinTask.adapt(() -> Arrays.parallelSort(objs))); // this hangs!
}
}
Update 1 (in response to Stephen C's comment):
Consider this example with considerable logic executed in a custom pool. This appears not only a "major loss of function" (hence "Priority=P3" on the Open-JDK ticket), because this worked in JDK16. It also displays an incompatibility within the JDK17 itself, because, we can't apparently use custom pools together with the collections framework anymore. I'm still not sure, how to solve this. The only thing I can conceive of is that anything that was originally designed to be run in the common pool must be explicitely submitted to the common pool, which would seem like a tough design choice. What am I overlooking?
new ForkJoinPool(1).invoke(ForkJoinTask.adapt(() -> {
// ... some really smart and deep business logic in a ginormous application using
// a custom ForkJoinPool, when an innocent developer or a library for that matter
// (not even realizing that it runs inside a custom ForkJoinPool) decides
// to use the collections framework ...
Arrays.parallelSort(Stream.generate(() -> "").limit(10_000).toArray(String[]::new));
// ... dead code from here ...
}));