Identifying shared data

A JVM instance hosts only one Java application. A Java application can create and execute multiple threads. When a new thread is created, it’s allotted its exclusive share in the memory. The JVM also allows partial runtime data to be shared between threads. A JVM’s runtime data set includes the method area, the heap, Java stacks, and PC registers (it also includes native method stacks). The method area includes class information that includes a note on all its variables and methods. This data set includes the static variables, which are accessible and shared by all the threads in a JVM. The static variables are shared by one class loader—that is, each class loader has its own set of static variables. The heap includes objects that are created during the lifetime of an application, again shared by multiple threads and processes. PC registers and Java stacks aren’t shared across the threads. On instantiation, each thread gets a PC register, which defines the next set of instructions to execute. Each thread is also assigned a Java stack. When a thread begins execution of a method, a method frame is created and inserted into the java stack, which is used to store the local variables, method parameters, return values, and intermediate values that might be used in a method. Of all this data, the method area and heap are shared across threads. Figure below shows this arrangement.

In the next section, you’ll see how multiple threads can interfere with each other’s working.

Last updated