当前位置:网站首页>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
边栏推荐
猜你喜欢

Several ways of sharing printers in LAN

Scalable storage system (I)

"Five layer" architecture of cloud applications and services

以自动化赋能转型,飞鹤乳业加速迈向数字化!

applicationContext.getBeansOfType 获取一个接口下所有实现类 执行方法或者获取实现类对象等 操作应用场景学习总结

MySQL configuration of database Series F5 load balancing

django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.3

电学基础知识整理(一)

Chapter IX app project test (3) test tools

数字电路学习笔记(一)
随机推荐
iptables防火墙规则和firewalld防火墙规则详解
JVM一:JVM入门以及Class文件认识
【毕业季】研究生の毕业总结
Typescript union type
leetcode:单调栈结构(进阶)
Simple implementation of cool GUI window based on WPF
Go speed
解析教育机器人的综合应用能力
数字有为,易步到位 华为携“5极”明星产品加速布局商业市场
Unity C# 网络学习(十一)——自定义协议生成工具
数据库
Pycharm setting pseudo sublime color scheme
Lamaba表达式学习及常用函数式接口
组件拆分实战
力扣每日一题-第29天-523.在区间范围统计奇数数目
多线程与高并发四:VarHandle与强软弱虚引用和ThreadLocal
多线程与高并发五:等待队列及Executor和线程池详解(重点)
WARN:  SQL  Error: …
KVM常用命令详解
Circular sliding auto adsorption UI tool that monkeys can use