当前位置:网站首页>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
边栏推荐
- How to write anti shake throttling for small programs?
- Paging query optimization in MySQL of database Series
- STM32 peripheral SDIO and SD card configuration
- English语法_形容词/副词3级-比较级_常用短语
- No  result  defined& nbsp…
- applicationContext.getBeansOfType 获取一个接口下所有实现类 执行方法或者获取实现类对象等 操作应用场景学习总结
- Huawei equipment WLAN basic service configuration command
- 黑体辐射初探
- 数字电路学习笔记(二)
- gcd最大公约数
猜你喜欢
随机推荐
数据库
第14章 AC-DC电源前级电路 笔记一
INFO:  HHH000397:  Using…
数据库系列之MySQL中的分页查询优化
Self use tool unity video player that monkeys can use
多线程与高并发六:线程池源码解析
小程序的防抖节流怎么写?
Li Kou daily question - day 29 -1491 Average wage after removing minimum wage and maximum wage
"9 No" principle and "5 measurement dimensions" of extensible system
A preliminary study of blackbody radiation
测不准原理
kubernetes资源对象介绍及常用命令
MySQL错误
光的粒子说(光电效应/康普顿效应)
MySQL 主从复制、分离解析
English语法_形容词/副词3级-比较级_常用短语
What is the difference between slice and array in go question bank 12?
Extensible database (I)
电学基础知识整理(一)
Documentation issues