In the Structure of Java Virtual Machine we scratched the surface of class file structure, how it is connected to java memory model via class loading process. Also we briefly discussed bytecode structure and its execution including short introduction to Just In Time runtime optimisation. In this post we will look more at internals of execution engine, however there there is no ambition to substitute a detailed VM implementation documentation for HotSpot JVM but just provide enough details to gain bigger picture.
Basic Threading model in HotSpot JVM is a one to one mapping between Java threads (an instance of java.lang.Thread) and native operating system threads. The native thread is created when the Java thread is started and reclaimed once it terminates. The operating system is responsible for scheduling all threads and dispatching to any available CPU. The relationship between java threads priorities and operating system thread priorities varies across operating systems.
HotSpot provides monitors by which threads running application code may participate in mutual exclusion (mutex) protocol. Monitor is either locked or unlocked. Only one thread may own the lock at any time. Only after acquiring ownership of the monitor thread may enter the critical section protected by this monitor. Critical sections are referred as synchronised blocks delineated by synchronised keyword.
Apart from application threads JVM contains also internal threads which can be categorised to following groups:
- VM Thread – responsible for executing VM operations
- Periodic task thread – thread executing periodic operations within the VM (singleton instance of WatcherThread)
- GC threads – threads of different types to support parallel an concurrent garbage collections
- Compiler threads – performs compilation of bytecode to native code at runtime (C1 and C2 JIT compilation threads)
- Signal dispatcher threads – thread waiting for processing directed signals and dispatches them to a java signal handling method
VM thread spends its time waiting for requested operations to appear in the operation queue (VMOperationQueue). Operation are typically passed to VM Thread because they require the VM to reach safepoint before they can be executed. When the VM is at safepoint all threads inside the VM have been blocked and any threads executing in native code are prevented from returning to the VM while the safepoint is in progress. This means that VM operation can be executed knowing that no thread can be in the middle of modifying heap. All threads are in a state such that their Java stacks are unchanging and can be examined.
Most familiar VM operation is related to garbage collection, particularly stop-the-world phase of garbage collection that is common to many garbage collocational algorithms. Other VM operation are: thread stacks dumps, thread suspension or stopping, inspection or modification via JVMTI etc. VM operation can by synchronous or asynchronous.
Safepoints are initiated using cooperative pooling based mechanism. Thread asks: “Should I block for a safepoint?” Moment when this is happening often is during thread state transition. Threads executing interpreted code don’t usually ask the question, instead when safepoint is requested interpreter switches to different dispatch table which includes that question. When safepoint is over dispatched table is switched back. Once safepoint has been requested VM Thread must wait until all threads are known to be in safepoint safe state before proceeding with operation. During safepoint thread lock is used to block any threads that were running and releasing lock when operation completed.