Does the JVM or base OS change the state of the thread

When I create a multi-threaded program, and I use methods such as Wait or Signal to control the flows, among other things, does the JVM manage the entire change in the state of the stream or does something have a base OS with it.

+4
source share
3 answers

It depends on the implementation of the JVM. Most modern JVMs (Suns HotSpot, Oracles JRockit, IBM VM) will use the operating system streaming model, as this will give better performance.

In earlier implementations, green threads were used - the virtual machine simulated the threads using it. This was commonly used when the platform or operating system on which it was running did not support streaming. For example, in Java 1.1, Solaris used green threads. At that time, the general scheme for using several cores / CPUs in Solaris was to use several processes - only later threads were added to the operating system.

The Java language specification does not specify how threads should be implemented, but in general, if the OS supports threads, the modern JVM will use the OS implementation. When there is no support in the OS, for example, on low-level mobile phones or, for example, in the implementation of a Java card, green threads will be used in the runtime environment.

+10
source

In general, Java threads will be displayed in OS threads, and Java will use OS synchronization primitives to implement the / wait / signal / ... synchronization, but matching is not as simple as you think. In fact, the JVM uses some tricky tricks to improve performance and implements as much of the synchronization code as possible (at least a non-conflict case).

If you're really interested in the details, check out the JVM source code or cmeerw.org/notes/java_sync.html for some overview of how Java synchronization primitives are implemented on Linux and Solaris.

+2
source

In the early days of Linux 2.4, at least the IBM JVM used separate processes to implement Java threads. This led to a lengthy switch between threads, as the system had to activate a completely different process each time.

+1
source

All Articles