当前位置:网站首页>Interviewer: let's talk about the process of class loading and the mechanism of class loading (parental delegation mechanism)
Interviewer: let's talk about the process of class loading and the mechanism of class loading (parental delegation mechanism)
2022-04-23 14:35:00 【Java ape~】
Catalog
When will class loading be performed ?
️ What is the parent delegate model ?
The advantages of the parental delegation model
When will class loading be performed ?
java Class name ,Java The entry class of the program , You need to load the class first and then execute main Method
When the program is running , perform Static method calls , Static variables and other operations
new object When
Create a class object through reflection , Then you can generate instance objects through reflection , Or call static methodsClass loading is performed only once , When class loading is completed , The method area will store class information , The heap will hold class objects
In multithreaded environment ,JVM Perform class loading When , Will use synchronized Lock Come on Ensure thread safety , Because class loading can only be performed once
The process of class loading
🧬 Learn about the learning class before loading it The life cycle of a class :

The first five steps are the process of class loading in order , The middle three steps belong to connection , So for class loading, the steps are as follows :
load
Connect : verification , Get ready , analysis
initialization
️ load
Be careful : The loading here should be distinguished from class loading , Loading is only one stage of the class loading process
During the loading phase ,JVM Virtual opportunity does the following three things :
Get the binary byte stream that defines this class by using the fully qualified name of a class
Convert the static storage structure represented by the byte stream into the runtime data structure of the method area
Generate a representation of this class in memory java.lang.Class Object is used as the access entry for various data of this class in the method area
In short , Just load class Bytecode ( binary data ) Method to area , A... Is generated in the heap Class object
️ Connect
🪞 verification
Verification is the first step in the connection , This stage is Guarantee Class file The information contained in the byte stream accord with java Virtual machine specification , Ensure that this information , Will not endanger the security of the virtual machine itself
🧭 Get ready
The preparation phase is formal In class Define sub variable ( Static variables ) Allocate memory and set Variable Initial value
For example, a piece of code like this :
public static int val = 10;
it Initialize to val The value of is 0, No 10, because 10 It was only after initialization that it was assigned 10 This value
The initial value of the object is null, The initial value of the basic data type is the basic type as the default value of the member variable , such as int The initial value is 0,boolean The initial value is false
️ analysis
The parsing phase is Java Virtual machines will Replace symbolic references in the runtime constant pool with direct references The process of , That is to say Initializing constants The process of
🧯 Symbol reference : Compilation of class In file , There are references to worthy correspondence , In the memory that has not been loaded at this time , Just use “ Symbol reference ” To express this relationship
🧯 Direct reference : Perform class loading , hold class After the bytecode is loaded into the memory , The relationship between variable and value embodied in memory is direct reference
️ initialization
Initialization phase ,Java The virtual machine really starts to execute... In the class Java Code , This stage is the process of executing the class constructor method
Static variables are only initialized and assigned at this stage
* Parent delegation model
Two points to know when learning the parental delegation mechanism :
Parent delegation model jdk The loading mechanism of the default class
Perform class loading , yes Java Virtual machines load classes through class loaders
️ What is the parent delegate model ?
If a class loader receives a class load request , it I won't try to load this class first , But put this Request delegate to parent loader To execute , The same is true for the parent loader that receives this request , Finally, the request will be delegated to the top-level startup class loader , Only When the parent loader feedback fails to load the request ( It did not find the required class in its search scope ) when , The child loader will try to load itself
BootStrap Start class loader (BootStrap ClassLoader)
Ext Extend the classloader (Extension ClassLoader)
App application / system class loader (Application ClassLoader)
Custom class loaders
From top to bottom corresponds to the hierarchical relationship from parent to child
Two exceptions that class loading must understand :
🪟ClassNotFoundException: Class cannot be found when loading class file
🪟NoClassDefFoundError: Class found class file , But class loading error , There is no class information in the method area , There are no class objects in the heap
The advantages of the parental delegation model
️ Avoid repeatedly loading classes : When a class is loaded by the parent class loader , It will not be sent to the subclass loader to perform loading
To ensure safety : Such as Object,String Classes and so on use jdk Provide classes instead of using self-defined java.lang.Object, If a hacker uploads one from the network java.lang.Object Binary data of , If there is no parental delegation mechanism, security is guaranteed , There will be security risks
Using the parental delegation mechanism , The priority of ensuring loading is jdk Class provided
Do not use the parental delegation mechanism , Load custom classes ,jdk There will also be secure authentication
版权声明
本文为[Java ape~]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231433426528.html
边栏推荐
- 机器学习之逻辑回归(Logistic Regression)原理讲解和实例应用,果断收藏
- 详解TCP的三次握手
- Proteus simulation design of four storey and eight storey elevator control system, 51 single chip microcomputer, with simulation and keil c code
- 51单片机的花卉、农田自动浇水灌溉系统开发,Proteus仿真,原理图和C代码
- 基于TLC5615的多路可调数控直流稳压电源,51单片机,含Proteus仿真和C代码等
- 拼接hql时,新增字段没有出现在构造方法中
- 8.2 文本预处理
- MDS55-16-ASEMI整流模块MDS55-16
- I thought I could lie down and enter Huawei, but I was confused when I received JD / didi / iqiyi offers one after another
- 阿里研发三面,面试官一套组合拳让我当场懵逼
猜你喜欢

c语言在结构体传参时参数压栈问题

555定时器+74系列芯片搭建八路抢答器,30s倒计时,附Proteus仿真等

API gateway / API gateway (IV) - use of Kong - Integrated JWT and fuse plug-in

Design of single chip microcomputer Proteus for temperature and humidity monitoring and alarm system of SHT11 sensor (with simulation + paper + program, etc.)

Notes on Visio drawing topology

C语言知识点精细详解——数据类型和变量【2】——整型变量与常量【1】

基于单片机的DS18B20的数字温度监控报警系统设计【LCD1602显示+Proteus仿真+C程序+论文+按键设置等】

机器学习之逻辑回归(Logistic Regression)原理讲解和实例应用,果断收藏

asp.net使用MailMessage发送邮件的方法
![Detailed explanation of C language knowledge points -- first knowledge of C language [1]](/img/7d/2fc527949562e1a7ef1525c7fced81.png)
Detailed explanation of C language knowledge points -- first knowledge of C language [1]
随机推荐
Basic regular expression
单相交交变频器的Matlab Simulink建模设计,附Matlab仿真、PPT和论文等资料
八路抢答器系统51单片机设计【附Proteus仿真、C程序、原理图及PCB文件、元器件清单和论文等】
Eight way responder system 51 Single Chip Microcomputer Design [with Proteus simulation, C program, schematic diagram, PCB files, component list and papers, etc.]
Some little records~
ArrayList collection basic usage
Want to be an architect? Tamping the foundation is the most important
asp.net使用MailMessage发送邮件的方法
1 minute to understand the execution process and permanently master the for cycle (with for cycle cases)
外包幹了四年,廢了...
ASEMI整流模块MDQ100-16在智能开关电源中的作用
OpenFaaS实战之四:模板操作(template)
【JZ46 把数字翻译成字符串】
Detailed explanation of SAR command
LLVM - 生成 if-else 以及 PH
关于UDP接收icmp端口不可达(port unreachable)
【工厂模式详解】工厂方法模式
压缩映射定理
Qt实战:云曦聊天室篇
AT89C52 MCU frequency meter (1Hz ~ 20MHz) design, LCD1602 display, including simulation, schematic diagram, PCB and code, etc