Search code examples
javamultithreadingjava-streamfork-joinforkjoinpool

Fork Join pool hangs


The case is that an application hangs infinitely from time to time.

Seems that the bug sits in the following snippet:

ForkJoinPool pool = new ForkJoinPool(1); // parallelism = 1

List<String> entries = ...;

pool.submit(() -> {

    entries.stream().parallel().forEach(entry -> {
        // An I/O op.
        ...
    });

}).get();

Thread pool-4-thread-1 that executes the code freezes on get():

"pool-4-thread-1" #35 prio=5 os_prio=0 tid=0x00002b42e4013800 nid=0xb7d1 in Object.wait() [0x00002b427b72f000]
       java.lang.Thread.State: WAITING (on object monitor)
        at java.lang.Object.wait(Native Method)
        at java.util.concurrent.ForkJoinTask.externalInterruptibleAwaitDone(ForkJoinTask.java:367)
        - locked <0x00000000e08b68b8> (a java.util.concurrent.ForkJoinTask$AdaptedRunnableAction)
        at java.util.concurrent.ForkJoinTask.get(ForkJoinTask.java:1001)
...other app methods

One can assume that the task passed to submit() executes too long.

But surprisingly there is no ForkJoinPool-N-worker-N occurrences in the thread dump, so looks like the pool doesn't perform any computations!

How is that possible? If no tasks are executed by the pool, why pool-4-thread-1 thread waits inside get()?

P.S. I know that it's not recommended to execute I/O-related tasks in ForkJoinPool, but still interested in the root of the problem.

Update. When parallelism is set to value greater than 1, no problems are detected.


Solution

  • Set parallelism = N where N > 1 solved the problem.

    Strange thing but seems that there is some bug in ForkJoinPool similar to what is stated here.