JVM——Class Loading Mechanism (3)

4. Class loader

1 Overview

2. Classes and class loaders

3. Parent delegation model

 

1 Overview

The virtual machine design team put the action of "getting the binary byte stream describing this class through the fully qualified name of a class" in the class loading phase to the outside of the Java virtual machine, so that the application can decide how to obtain the required the type. The code module that implements this action is called a "class loader".

2. Classes and class loaders

Although the class loader is only used to implement the loading action of the class, its role in the Java program is far beyond the class loading stage. For any class, its uniqueness in the Java virtual machine needs to be established by the class loader that loads it and the class itself . Each loader has an independent class namespace.

In layman's terms: comparing whether two classes are "equal" only makes sense if the two classes are loaded by the same class loader. Otherwise, even if the two classes come from the same Class file, they are A virtual machine loads, as long as the class loaders that load them are different, the two classes must not be equal.

The "equality" referred to here includes the equals() method of the Class object representing the class (the entry of the class mentioned earlier), the return result of the isAssignableForm() method, and the use of the instanceof keyword to determine the relationship of objects. .

3. Parent delegation model

From the perspective of the Java virtual machine, there are only two different class loaders: one is the Bootstrap ClassLoader, which is implemented in C++ and is part of the virtual machine itself; the other is the Bootstrap ClassLoader. That is, all other class loaders, which have Java preamble implementations, are independent from outside the virtual machine, and all inherit from the abstract class java.lang.ClassLoader.

Details can be divided into three categories:

Bootstrap ClassLoader

②Extension ClassLoader

③ Application class loader (Application ClassLoader): Also known as system class loader. It is responsible for loading the class library specified on the user's classpath (classPath), and developers can use this class loader directly.

This hierarchical relationship between class loading as shown in the figure is called the Parents Delegation Model of the class loader .

The parent delegation model requires that all class loaders except the top-level class loader should have their own parent class loader. The parent-child relationship between class loaders here is generally not implemented in an inheritance relationship, but uses a combination relationship to reuse the code of the parent loader.

The working process of the parent delegation model is: if a class loader receives a class loading request, it will not try to load the class by itself first, but delegate the request to the parent class loader to complete, and each level of class The same is true for loaders, so all load requests should eventually be sent to the top-level startup class loader, only when the parent loader reports that it cannot complete the load request (the required class is not found in its search scope) , the subloader will try to load it by itself.

Benefit : Java classes have a prioritized hierarchy along with their class loader. For example, no matter which class is loaded to load the java.lang.Object class, it will eventually be done by the startup class loader dispatched to the top, so the java.lang.Object class is the same in each class loader environment of the program. kind. If the parent delegation model is not used, and each class loader loads it by itself, if the user writes a class called java.lang.Object and puts it in the ClassPath of the program, there will be multiple different classes in the system. The Object class, the most basic behavior in the Java type system, cannot be guaranteed.

Logic implementation : first check whether it has been loaded, if not, call the loadClass() method of the parent loader, if it is empty, the startup class loader is used as the parent loader by default. If the parent class fails to load, throw a ClassNotFoundException, and then call its own FindClass() method to load it.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325373327&siteId=291194637
Recommended