Introduction of java virtual machine

Introduction of java virtual machine


1. Basic structure of JVM 1. Class loader
2. Execution engine
3. Run-time data area
4. Native library interface

Here are some pictures drawn by others:
Borrow other people's pictures

Class Files ----> ClassLoader---->Runtime Data Area---->Execution Engine, Native Library Interface----->Local Method Library

Second, the loading sequence of the
class : 1. Class loading,
loading, connecting (verification, preparation, analysis), initialization, use, unloading; Class saves the definition or structure of the class in the heap;
2. Initialization:
executes the class constructor, The static variables of the class are assigned the correct initial values.
Constructor:

1.static变量
2.static{}语句

In order to verify whether it is consistent with the above loading sequence, we write the code of the segment test:

public class Test {
    
    
    // 先写静态方法
    static {
    
    
        total = 3;
    }
	// 再写静态类成员变量
    private static int total = 2;
    public static void main(String[] args) {
    
    
        System.out.println(total);
        total = 4;
        System.out.println(total);
    }
}

The output result is 2 first, then 4;

3. The existing class loader and parent delegation mode of JDK:

  • Start class loader: BootstrapClassLoader
    is a class loader implemented with native code, which is responsible for
    loading the class library under <Java_Runtime_Home>/lib into memory (such as rt.jar). Since the boot class loader involves the local implementation details of the virtual machine, developers cannot directly obtain a reference to the startup class loader, so direct operations by reference are not allowed.

  • Standard extension (Extension) class loader: ExtClassLoader
    is implemented by Sun's ExtClassLoader (sun.misc.Launcher$ExtClassLoader). It is responsible for loading into memory the class library in <Java_Runtime_Home>/lib/ext or the location specified by the system variable java.ext.dir. Developers can directly use the standard extended class loader.

  • System class loader: AppClassLoader
    is implemented by Sun's AppClassLoader (sun.misc.Launcher$AppClassLoader). It is responsible for loading the class library specified in the system classpath (CLASSPATH) into memory. Developers can directly use the system class loader.

  • Custom Loader---->Custom Load Path

Using a particularly simple example, through this simple example, look at the order of the loader that executes the call:

public class Test {
    
    
    public static void main(String[] args) {
    
    
        ClassLoader classLoader = Test.class.getClassLoader();
        System.out.println(classLoader.toString());
        System.out.println(classLoader.getParent());
        System.out.println(classLoader.getParent().getParent());
    }

}

Implementation result: Surprise is not a surprise, it is amazing, I accidentally saw the model bird appointed by the parents!

sun.misc.Launcher$AppClassLoader@18b4aac2
sun.misc.Launcher$ExtClassLoader@3b07d329
null

Introduction to the Parental Delegation Model: Regardless of what is said below, it is actually very simple. I will tell you the story of dumping the pot.
One day, there was a class that needed AppClassLoader to load itself. AppClassLoader was impatient and said to go to my ExtClassLoader; ExtClassLoader is not the owner of the service, but also said to go to my BootstrapClassLoader. Think about how hard this old man is!
Sure enough, one day, the old man BootstrapClassLoader is so tired and crooked. Uncle, I won't play anymore. You can play ExtClassLoader yourself! So ExtClassLoader did it by itself.
But the weather is unpredictable, ExtClassLoader is sick one day and can’t do it anymore. There is no other way but to find appClassLoader and tell it: "My son, I can’t do it, you can do it yourself!"
Hahaha...that's probably it A situation.

1.当ApplicationClassLoader 收到一个类加载请求时,他首先不会自己去尝试加载这个类,而是将这个请求委派给父类加载器ExtensionClassLoader去完成。
2.当ExtensionClassLoader收到一个类加载请求时,他首先也不会自己去尝试加载这个类,而是将请求委派给父类加载器BootstrapClassLoader去完成。
3.如果BootstrapClassLoader加载失败(在<JAVA_HOME>\lib中未找到所需类),就会让ExtensionClassLoader尝试加载。
4.如果ExtensionClassLoader也加载失败,就会使用Application ClassLoader加载。
5.如果ApplicationClassLoader也加载失败,就会使用自定义加载器去尝试加载。

3. Runtime data area: The
java virtual machine divides the memory it manages into several different data areas during the execution of the java program. These areas have their own purposes, as well as the time of creation and destruction. Some areas always exist as the virtual machine process starts, and some areas are created and destroyed depending on the start and end of the user thread.
The division of the data area is roughly as shown in the following figure:
Blue: The data area shared by all threads always exists as the virtual machine process starts.
Orange: thread-private isolated data area, created and destroyed with the start and end of user threads
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_37488998/article/details/109764361