当前位置:网站首页>Talk about the parental delegation mechanism in detail (often asked in interviews) [easy to understand]
Talk about the parental delegation mechanism in detail (often asked in interviews) [easy to understand]
2022-07-23 19:35:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm your friend, Quan Jun .
Preface
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
working principle
(1) 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
(2) 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 ;
(3) 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
(4) 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
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(" I'm custom 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 StringTest {
public static void main(String[] args) {
java.lang.String str = new java.lang.String();
System.out.println("hello,atguigu.com");
StringTest test = new StringTest();
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(" I'm custom 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
Through the example above , We can know , The parental mechanism can
- Avoid duplicate loading of classes
- Protect program security , Prevention core API Be tampered with at will
- Custom class :java.lang.String ( useless )
- Custom class :java.lang.ShkStart( Report errors : Block creation java.lang Initial class )
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/126800.html Link to the original text :https://javaforall.cn
边栏推荐
猜你喜欢

基于自学习的机器人决策系统(达闼科技赵开勇)

MySQL 啥时候用表锁,啥时候用行锁?

Publish the local image to Alibaba cloud warehouse

Four principles of interface design

FPGA flash reading and writing based on SPI

结构体大小的计算(结构体内存对齐)

Little fish sends lidar | just dinner is the first lottery

二、MFC窗口和消息

为啥一问 JVM 就 懵B ?

PowerCLi 管理VMware vCenter 一键批量部署OVF
随机推荐
图学习总结
四旋翼飞行器1——结构和控制原理
Alibaba's latest masterpiece! It took 187 days for the liver to come out. 1015 pages of distributed full stack manuals are so delicious
J9数字论:数字行业的FOMO现象我们应该怎么克服?
As a background developer, you must know two kinds of filters
idea 选中代码生成方法
R语言检验样本是否符合正态性(检验样本是否来自一个正态分布总体):使用nortest包的sf.test函数检验样本是否符合正态分布(normality test)
关于:在企业局域网中启用 Delivery Optimization
R语言ggplot2可视化:使用ggplot2可视化散点图、使用ggpubr包的theme_classic2函数设置可视化图像为经典主题(classic theme with axis lines)
看完这篇,彻底搞懂 gRPC!
not all arguments converted during string formatting
【pm2】pm2常用命令
Basic process of process scheduling
【C语言】程序环境和预处理
Figure learning summary
TCL scripting language foundation (2)
什么是弱网测试?为什么要进行弱网测试?怎么进行弱网测试?「建议收藏」
PC性能监测工具,软件测试人员不可或缺的好帮手
GPS北斗时钟服务器(NTP网络时钟系统)施工部署方案
Codeforces Round #809 (Div. 2)【VP记录】