当前位置:网站首页>Thread operation principle

Thread operation principle

2022-06-24 10:33:00 Sit at a sunny window and drink tea alone

Stack and stack frame

Java Virtual Machine Stacks (Java Virtual machine stack )

  • JVM Zhongyou Pile up 、 Stack 、 Method area The composition of , Stack memory is used by threads ,
  • After each thread starts , The virtual machine will allocate a piece of stack memory .
  • Each stack consists of multiple stack frames (Frame) form , It corresponds to the memory occupied by each method call
  • Each thread can only have one active stack frame , Corresponds to the currently executing method

Specific examples are as follows :

package cn.knightzz.example.e2.source;

public class TestFrame {
    


    public static void method1(int n) {
    

        Object m = method2(n + 1);
        System.out.println("m = " + m);

    }

    public static Object method2(Object n) {
    
        return n;
    }

    public static void main(String[] args) {
    
        method1(10);
    }
}

When we run to the main thread method1(10) when , You can see that only the main thread is active

img

And when I run to method2(n+1) When in position The current active stack frame corresponds to method1

img

Continue running until method2 You can see that there are three stack frames at this time , Active stack frame is method2, therefore

  • Each stack consists of multiple stack frames (Frame) form , It corresponds to the memory occupied by each method call
  • Each thread can only have one active stack frame , Corresponds to the currently executing method

img

Stack frame diagram

Code example

package cn.knightzz.example.e2.source;

/** * @author  Wang Tianci  * @title: TestFrame * @projectName hm-juc-codes * @description:  Thread stack frame  * @website <a href="http://knightzz.cn/">http://knightzz.cn/</a> * @github <a href="https://github.com/knightzz1998">https://github.com/knightzz1998</a> * @create: 2022-06-15 22:18 */
public class TestFrame {
    


    public static void method1(int n) {
    

        Object m = method2(n + 1);
        System.out.println("m = " + m);

    }

    public static Object method2(Object n) {
    
        return n;
    }

    public static void main(String[] args) {
    
        method1(10);
    }
}

The initial state is as follows :

  • All the code is loaded into the method area , JVM Virtual machine allocates stack memory for main program

img

Execute main method , establish main Stack frame

img

Local variable table :

  • Store local variables and method parameters , about main In terms of functions , The local variable table stores args
  • args What is stored is the command information of the executing program : java -jar -Xmx2G The following parameter information

Keep going down , Execute to method1 In the method , Note that at this time :

  • The program counter records the position of the program at this time : method1(10), As you continue down , The program counter records every line of code executed
  • The return address points to the method area main Function position method1(10) Location
  • Local variables are stored n and m Two variables , One is method input parameters , One is the object
  • Be careful : Objects are stored in the heap

img

As we continue down , Execute to method2 :

  • The return address points to method1 Call in method2 The location of

img

When the last method method2 After execution , It will return step by step according to the return address , Until the end

原网站

版权声明
本文为[Sit at a sunny window and drink tea alone]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206240926141383.html