ForkJoinPool has a common parallel "number of cores - 1", respectively, if there are 4 cores, then out of 4 launched threads 1 will alternate with the rest in the execution on the core. But why if I create my own FJP, and I give it parallelism equal to 4, then all the same the same 4 threads run on 3 cores? I checked it this way: I ran a volume task for each of the 2 threads - in each thread I completed in 3.3 seconds, I started the task for 3 threads - the same 3.3 seconds for each thread, I ran the task for 4 threads - everything, each task took -5 sec.
Does the JVM reserve itself even one core on the GC and so on?

  • initialization time is not taken into account. - Roman C
  • one
    What makes you think that it has parallel "number of cores - 1" if the default constructor looks like this: public ForkJoinPool () {this (Math.min (MAX_CAP, Runtime.getRuntime (). AvailableProcessors ()), defaultForkJoinWorkerThreadFactory, null, false); } - Ivan Gladush
  • @RomanC, it really is not accepted. The countdown began specifically from the beginning of the task, and not from the beginning of the flow, and so on - Oleg Rozdaybeda
  • @IvanGladush, based on our own tests, because of the call to .toString (), through which it is indicated that concurrency = 3, also based on this topic: stackoverflow.com/questions/37494811/… " - Oleg Rozdaybeda
  • How did you divide the task? How many tasks is a big task? Considered that the optimal task should be divided with a correction factor of 10..100? - dSH

1 answer 1

  1. The general answer is probably this - it depends on the implementation of JVM.
  2. When you create your ForkJoinPool, two ForkJoinPool appear in the system, because one system is used for example in StreamAPI. In such a case, scheduling CPU utilization becomes much more difficult. Therefore, to create another ForkJoinPool you need to have a specific rationale. It is recommended to use the system
  3. We still need to look at how the task was broken. the optimal ratio - the size of the problem is divided by the number of cores, and then also by the correction factor 10..100. Otherwise, either the cores will not be involved, or most of the time will not go to the task, but to divide it into subtasks.
  4. Care should be taken to measure, measured "on the knee." Too many pitfalls in this thread. And of course, you need code to understand what has been measured, preferably with an indication of the specific JVM and operating system, the existing hardware.

But if we talk about tests "on the knee", this is what the ForkJoinPool test launched on my machine: Arbitrary ForkJoinPool Test

I ask you not to pay attention to specific numbers - it's just the time of execution. Relative indicators are important. Winning with parallelism = 4 is there, just here, as in many other areas of life, the law of marginal utility is observed - each next unit of the resource brings less and less benefit. I was surprised that the seven with parallelism = 1 turned out to be better.

Here is the load on the process - i.e. In my case, the load is full with parallelism 4: enter image description here

  • Parallelism can be measured on the knee, especially if the tasks are large. And if the JVM implementation is OracleJDK and now the API of the FJP does not do anything now, then why doesn’t normal parallelization occur over 4 cores? - Ivan Gladush
  • And how many cores do you have on iron - 4? - dSH
  • Yes, I have 4 cores on the gland, without hyperthreads - Ivan Gladush
  • report the remaining parameters at once - what OS, what version of jvm and if possible a code - I will try to experiment on weekends - dSH
  • horizontal parallelism of course - dSH