22.2 Runtime Organization for Thread Execution
Most JVM implementations run as a single process, but allow multiple threads to be created. Runtime organization for thread execution in the JVM is depicted in Figure 22.1. The JVM has designated memory areas, called runtime data areas, that are deployed for various purposes during program execution. Figure 22.1 shows data areas that are created specifically for each thread and are private to each thread. The figure also shows data areas that are shared by all threads, called shared memory.
Figure 22.1 Runtime Data Areas
Each thread has the following data areas, which are private to the thread:
- JVM stack: A JVM stack (also known as execution stack, call stack, or frame stack) is created for each thread when the thread starts, and is used for bookkeeping method executions in the thread, as explained in §7.1, p. 365. This is also where all local variables for each active method invocation are stored. Note that each thread takes care of its own exception handling, and thus does not affect other threads.
- Program counter (PC): This register is created for each thread when the thread starts, and stores the address of the JVM instruction currently being executed.
The following data areas are shared by all threads:
- Heap: This shared memory space is where objects are created, stored, and garbage collected.
- Method area: This is created when the JVM starts. It stores the runtime constant pool, field and method information, static variables, and method bytecode for each of the classes and interfaces loaded by the JVM.
- Runtime constant pool: In addition to storing the constants defined in each class and interface, this also stores references to all methods and fields. The JVM uses the information in this pool to find the actual address of a method or a field in memory.
Threads make the runtime environment asynchronous, allowing different tasks to be performed concurrently. Using this powerful paradigm in Java centers on understanding the following aspects of multithreaded programming: