[Java virtual machine principle] thread stack | stack frame | local variable table | disassembly bytecode file | Java virtual machine instruction manual | program counter





1, Thread stack


Load HelloWorld When the class bytecode file is stored in the memory of the Java virtual machine, the data in the bytecode file will be decomposed and placed in different memory areas;

public class HelloWorld {

    public int add() {
        int a = 1;
        int b = 1;
        int c = a + b;
        return c;
    }

    public static void main(String[] args) {
        HelloWorld helloWorld = new HelloWorld();
        helloWorld.add();
    }
}

Run the HelloWorld Class bytecode file, a process will be created;

java HelloWorld.class

The main method is the program entry. After running, a thread will be created, which is the main thread of the program;

public static void main(String[] args) {

At this time, the thread stack of the main thread will be created for the main thread;


For each thread, a thread stack should be created;





2, Stack frame


Create a thread stack unique to the main thread, which mainly stores "stack frame". Each method corresponds to a stack frame. Here, the stack frame corresponding to the main method is stored. The stack frame stores temporary variables and operands;

"Stack frame" is the same as the stack in the data structure, first in first out, last in first out;

In the main thread stack, execute the main function and put the stack frame of the main method, then create a HelloWorld object, execute the add method of the object and put the stack frame of the add method;

The "stack frame" is managed in a stack manner in the thread stack, and the incoming stack frame is executed first. After execution, it is removed from the thread stack;


"Stack frame" stores local variable table, operand stack, dynamic link and method exit;





3, Stack frame - local variable table


Local variable table:

Take the following method as an example:

    public int add() {
        int a = 1;
        int b = 1;
        int c = a + b;
        return a + b;
    }

The local variable table, which stores local variables, is a, B, C in the above method, 3 3 3 local variables;


In the local variable table of the stack frame of the main method, store the local variable HelloWorld; However, note that the data storage location of the HelloWorld object is the heap;

    public static void main(String[] args) {
        HelloWorld helloWorld = new HelloWorld();
        helloWorld.add();
    }




4, Disassembly bytecode file


use

javac HelloWorld.java

Command, set HelloWorld Java compiled as HelloWorld Class bytecode file


use

javap -c HelloWorld.class

Command, for HelloWorld Class bytecode file for disassembly;

D:\java>javap -c HelloWorld.class
Compiled from "HelloWorld.java"
public class HelloWorld {
  public HelloWorld();
    Code:
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object."<init>":()V
       4: return

  public int add();
    Code:
       0: iconst_1
       1: istore_1
       2: iconst_1
       3: istore_2
       4: iload_1
       5: iload_2
       6: iadd
       7: istore_3
       8: iload_3
       9: ireturn

  public static void main(java.lang.String[]);
    Code:
       0: new           #2                  // class HelloWorld
       3: dup
       4: invokespecial #3                  // Method "<init>":()V
       7: astore_1
       8: aload_1
       9: invokevirtual #4                  // Method add:()I
      12: pop
      13: return
}





5, Java virtual machine instruction manual


The results of disassembly are Java virtual machine instructions; These instructions are given to the Java virtual machine for execution;

Analyze the above Java virtual machine instructions according to the Java virtual machine instruction manual;

A Java virtual machine instruction manual is attached, which can be downloaded from the blog resources;





6, Program counter


CPU time slice rotation: suppose there is a single core CPU, and each thread will be divided into a running time;

Split 1 second into 1000 parts, each 1ms; Many threads are striving for CPU resources. The operating system needs to allocate CPU time to each thread. For example, 3ms is allocated to thread 1 and 5ms is allocated to thread 2. After thread 1 is executed, it will immediately switch to thread 2 for execution. After thread 2 is executed, other threads will continue to seize it immediately;


Thread 1 executes for 3ms, and then the CPU runs thread 2. If thread 1 runs again after x ms, it needs to record which JVM instruction should be executed by the program counter;


When multiple threads run concurrently, they cross each other to seize CPU resources. After the thread executes the allocated CPU time, it is necessary to record where it is currently running and which JVM instruction to continue to execute after allocating CPU resources next time. Here, the program counter is required to realize this function;


The program counter is the number before recording the following JVM instructions;

  public int add();
    Code:
       0: iconst_1
       1: istore_1
       2: iconst_1
       3: istore_2
       4: iload_1
       5: iload_2
       6: iadd
       7: istore_3
       8: iload_3
       9: ireturn

Keywords: Java

Added by ensanity on Wed, 22 Dec 2021 03:26:19 +0200