当前位置:网站首页>Understanding and learning of parental delegation mechanism
Understanding and learning of parental delegation mechanism
2022-06-28 03:53:00 【Daxiong who loves learning】
List of articles
Reference article
Preface
The parental delegation mechanism is learning jvm Very important knowledge in the process !!
Java Virtual machines are right class The document uses Load on demand The way , That is to say, the class will be used only when it is needed class File loading into memory generation class object , and , Load the of a class class When you file ,Java The virtual machine uses Parent delegate mechanism , namely Leave the request to the parent class , It's a task delegation model
In introducing the parental delegation mechanism , Have to mention ClassLoader( Class loader ),JVM Three layers are provided in ClassLoader:
- Bootstrap classLoader: Mainly responsible for loading the core class library (java.lang.* etc. ), structure ExtClassLoader and APPClassLoader.
- ExtClassLoader: Mainly responsible for loading jre/lib/ext Some of the extended jar.
- AppClassLoader: It is mainly responsible for loading the main function class of the application
principle
- If a class loader receives a class load request , It doesn't load itself first , Instead, the request is delegated to the loader of the parent class
- If the parent loader still has its parent loader , Then further entrust , Recursion in turn , The request will eventually reach the top-level boot class loader ;
- If the parent loader can complete the class loading task , You're back , If the parent loader cannot complete the loading task , Sub loader will try to load by itself , This is the parent delegation mechanism
- The parent loader allocates tasks one layer at a time , If the subclass loader can load , Then load this class , If the load task is assigned to the system class, the loader cannot load this class , Throw an exception
Icon 1:

Icon 2:

Code example
give an example 1
I built one myself java.lang.String class , write static Code block
package java.lang;
public class String {
static{
System.out.println(" Self defined String Static code block of class ");
}
}
Load... In another program String class , Look at the loaded String Class is JDK Self contained String class , We wrote it ourselves String class
public class Test1 {
public static void main(String[] args) {
java.lang.String str = new java.lang.String();
System.out.println("hello World");
Test1 test = new Test1();
System.out.println(test.getClass().getClassLoader());
}
}
The program doesn't output the contents of our static code block , So what's still loaded is JDK Self contained String class 
Why? ?
Because we define String Class loader of this application system , But it doesn't load itself first , Instead, the request is delegated to the loader of the parent class , To the extension class loader discovery String Class is not under your own control , Then delegate to the parent class loader ( Boot class loader ), It turns out that java.lang package , This belongs to the boot loader , So what's loaded is JDK Self contained String class
give an example 2
In our own String Entire in class main() Method
package java.lang;
public class String {
static{
System.out.println(" Self defined String Static code block of class ");
}
// error : In the class java.lang.String I can't find it in China. main Method
public static void main(String[] args) {
System.out.println("hello String");
}
}
Because the parent delegation mechanism finds JDK Self contained String class , But in the core class library of the boot class loader API Inside String There is no main() Method 
give an example 3
stay java.lang Wrap the whole ShkStart class ( Custom class name )
package java.lang;
public class ShkStart {
public static void main(String[] args) {
System.out.println("hello!");
}
}
Out of the protection mechanism ,java.lang We are not allowed to customize classes under the package 
The advantages of parental appointment mechanism
Avoid duplicate loading of classes .
Once a class is loaded by the parent loader , It will no longer be delegated to subclasses to load
Protect program security , Prevention core API Be tampered with at will , Prevent the implantation of dangerous codes
When a class needs to be loaded , The first To try to load is BootstrapClassLoader, So other classloaders have no chance to load again System level classes , To a certain extent, it prevents the implantation of dangerous code .
Custom class :java.lang.String ( Will not cover jdk Of String class , useless )
Custom class :java.lang.ShkStart( Report errors : Block creation java.lang Initial class )
summary

In short , The parent delegation mechanism first delegates the parent class to load from the bottom up , After encountering the top level or failing to load the parent class of this class , Start loading from itself , If it doesn't load , Throw an exception
边栏推荐
猜你喜欢

加法器—笔记

"9 No" principle and "5 measurement dimensions" of extensible system

MySQL 主从复制、分离解析

ambari SSLError: Failed to connect. Please check openssl library versions.

Anaconda command usage

Scalable storage system (I)

基于 LNMP 搭建个人网站的填坑之旅

光的粒子说(光电效应/康普顿效应)

Arrangement of basic electrical knowledge (I)

MySQL configuration of database Series F5 load balancing
随机推荐
Circular sliding auto adsorption UI tool that monkeys can use
Li Kou daily question - day 29 -575 Divide candy
Web APIs DOM-事件基础丨黑马程序员
Sublime text 3 basic configuration tutorial
MySQL configuration of database Series F5 load balancing
vscode中出现无法在只读编辑器中编辑
Chapter IX app project test (3) test tools
A pit filling trip based on LNMP to build a personal website
leetcode:494.数组中添加加减运算符得到指定值的所有方法
kubernetes资源对象介绍及常用命令
A solution to the inefficiency of setting debug mode in developing flask framework with pychar
Pycharm不同项目之间共用第三方模块
数据库系列之MySQL中的执行计划
一文告诉你什么是 Kubernetes
Paging query optimization in MySQL of database Series
database
How to modify a se38 editor theme
可扩展数据库(下)
Lamaba表达式学习及常用函数式接口
django. core. exceptions. ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.3