5

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'?

Henry Twist
  • 5,666
  • 3
  • 19
  • 44
postFix
  • 361
  • 2
  • 10
  • This discussion is useful [finding-number-of-cores-in-java](https://stackoverflow.com/questions/4759570/finding-number-of-cores-in-java) – the Hutt Apr 04 '21 at 16:02

2 Answers2

5

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.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
2

The Answer by Stephen C is true today, where Java threads are typically implemented as native threads provided by the host operating systems. But things change with Project Loom technology being developed for a future version of Java.

Project Loom brings virtual threads (fibers) to the concurrency toolbox of Java. Many virtual threads will be mapped to each of the few platform/kernel threads. A virtual thread when blocked (waiting on a call to slow resources such as file I/O, network I/O, database access, etc.) will be “parked” (set aside) allowing some other virtual thread to execute for a while on the “real” platform/kernel thread.

This parked/unparked switching will be very fast, and take little memory (using a flexible growing/shrinking stack). This makes threads “cheap”, so cheap that you might reasonably be able to run millions of threads at a time.

Returning to your question, the management of these virtual threads will be managed within the JVM rather than the host OS. But underneath our virtual threads, we rely on the same platform/kernel threads used today, and those are ultimately controlled by the host OS rather than the JVM.

By default, when setting up an executor service backed by virtual threads via Executors.newVirtualThreadExecutor we do not specify the number of platform/kernel threads to be used. That s handled by the implementation of your JVM.

Experimental builds of Project Loom are available now, based on early-access Java 17. The Loom team seeks feedback.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • @StephenC Yes, I tried to make that clear too. But under Loom with virtual threads, the JVM will have much more involvement in managing actual work done. – Basil Bourque Apr 06 '21 at 01:53