Some threads in Java ForkJoinPool are idle from time to time

I have Java code as shown below:

Stream<String> stream = getStreamFromSomewhere()
ForkJoinPool pool = new ForkJoinPool(32);
pool.submit(() -> stream.parallel()
                        .filter(condition)
                        .forEach(x -> recursive(x))).get();
pool.shutdown();

      

Now this code is fast enough using all 32 threads on my machine, each of which is about 70-80% used.

Oddly enough, every few minutes the CPU uses all threads down to 0%, except for 2 threads (or sometimes 1) which reach 100%. After a minute or so, it would be okay and they were all humming 70-80% again. This behavior repeats every few minutes or so.

I can't pinpoint exactly what might be causing this. Any ideas would be helpful.

+3


source to share


2 answers


Different Java threads do not have to run on different CPU threads. Your 32 Java threads could theoretically allocate the same 1/32 times on one CPU thread. It would be somewhat wasteful if the remaining 31 CPU threads were unused, but still possible.

Exactly how tasks are divided into CPU threads depends on the scheduler . In your case, the scheduler, for whatever reason, decides that it needs to split 32 Java threads into two CPU threads.



I don't even know how you would start debugging something like this. It's out of your control and (almost) always will be, so why bother with it?

0


source


Anything else running on your computer has a higher priority than your program, so the processor can switch to that instead of your empty tasks.



0


source







All Articles