Does the decision of how many cores to use stay in the hands of the JVM?

Suppose we have a very complex task. I know that if we use one thread then in practice we will use one core, but if I divide the task into threads equal to the number of processor cores does the program necessarily run on all the cores?
Or there is no correlation between the number of threads and cores used and the JVM ‘decides’?


Actually, it is typically the Operating System that decides how many cores that a Java application gets to use. The scheduling of native threads to cores is handled by the operating system1. The JVM has little (if any) say thread scheduling.

But yes, an Java application won’t necessarily get access to all of the cores. It will depend on what other applications, services, etc on system are doing. Indeed, the OS may provide ways for an administrator to externally limit the number of cores that may be used by a given (Java or not) application, or give one application priority over another.

… or there is no correlation between the number of thread and used core’s

There is a correlation, but not one that is particularly useful. A JVM won’t (cannot) use more cores than there are native threads in existence (including the JVM’s internal and GC threads).

It is also worth noting that the OS (typically) doesn’t assign a core to a native thread that is not currently runnable.

Basil Bourque notes in his answer that Project Loom will bring significant improvements to threading in Java, if and when it is incorporated into the standard releases. However, Loom won’t alter the fact that the number of physical cores assigned an application JVM at any given time is controlled / limited by the OS.

1 – Or the operating system’s hypervisor. Things can get a bit complicated in a cloud computing environment, for instance.