Java class loader and parent-parent Delegation Model, java Delegation

Source: Internet
Author: User

Java class loader and parent-parent Delegation Model, java Delegation

Class and Class Loader

Although the class loader is only used for loading actions of classes, its role in Java programs is far from limited to the class loading stage. for any class, the class loader that loads it needs to establish its

The uniqueness of the Java Virtual Machine. In other words, it makes sense to compare the two classes if they are "equal" only when they are loaded by the same loader, otherwise, even if the two classes come from the same class (full qualified name), as long as the class loaders to load them are different, they cannot be said to be "equal.

Parent-Child Assignment Model

Types of Class 1 Loaders

From the perspective of a Java virtual machine, the loader only has the start class loader and other loaders (except the start class loader)

2. However, from the perspective of Java developers, the class loaders can be more detailed. Most Java programmers will use the following three loaders:

2.1 start the BootStrap ClassLoader class loader to load files in the JAVA_HOME \ lib directory.
2.2 Extension ClassLoader is responsible for loading problems in the JAVA_HOME \ lib \ ext directory.
2.3 The Application ClassLoader class loader is responsible for loading the class libraries specified by the user ClassPath. developers can directly use this loader, if no class loader is defined in the application, this class loader is used by default.

All applications are loaded by the above three types of loaders. If necessary, developers can add their own extended class loaders. The relationships between these class loaders are shown in:

The hierarchical relationship between the class loaders shown in the figure becomes the Parents Delegation Model of the class loaders ). the Parent-class delegation model requires that all other class loaders except the top-level start class loaders have their own parent class loaders.

Here, the parent-child relationship of the Class Loader is generally not implemented by the inherited relationship, but all uses the composite relationship to reuse the code of the parent loader.

Working Process of the parent-child Assignment Model

If a Class Loader receives a class load request, it first does not try to load the class itself, but delegates the request to its parent class loader to complete it, this is true for class loaders at each layer. Therefore, all requests are finally sent to the top-layer startup class loaders,

Only when the parent class loader reports that it cannot complete the load request will the subclass loader attempt to load it by itself.

The obvious advantage of using a parent-parent delegate model to organize the relationship between class loaders is that Java classes have a hierarchical relationship with priority along with their class loaders, the parental delegation model is very important to ensure the stable operation of Java programs, but its implementation is very simple.

The code for implementing the parent-parent delegate model is in the loadClass () method of java. lang. ClassLoader.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.