当前位置:网站首页>Class loading mechanism and parental delegation mechanism
Class loading mechanism and parental delegation mechanism
2022-07-24 14:16:00 【Shanghai city flower】
One 、 Class loading
summary
1: When a program wants to use a class , Class is not loaded into memory , The system will initialize the class in three steps ( load - Connect - initialization )
2: Get the class loader :Class#getClassLoader()
3: Class loader (class loader) Used to load Java Class to Java In the virtual machine . Generally speaking ,Java Virtual machine usage Java Class as follows :Java Source program (.java file ) after Java After the compiler compiles, it is converted to Java Byte code (.class file ). Class loader is responsible for reading Java Byte code , Translates into java.lang.Class An instance of a class .
Any class , It needs to be established by the class loader that loads it and the class itself java Uniqueness in virtual machines .
1. load
- That is to say class File read into memory (JVM Method area ), And create a class object ( Member variables , Member method , Construction method ).
- Any class used by the system will create a class object .
2: Connect
- verification Whether there is correct internal structure , And coordinate with other classes
- Get ready Responsible for allocating memory for static members of a class ( Static is loaded as the class is loaded ), And set the default initialization value
- analysis Replace the symbol reference in the binary data of the class with a direct reference .
3: initialization
Class loading ( initialization ) opportunity
- Create an instance of a class
- Access static variables of the class or assign values to static variables
- Calling static methods of a class
- Use reflection methods to force the creation of a class or interface corresponding to java.lang.Class object
- Initializing a subclass of a class
- Use it directly java.exe Command to run a main class
Composition and classification of class loaders
- Class loading mechanism : Will be class File loaded into memory , And generate corresponding class object ,
- Root loader > Extend the classloader > system class loader
Root loader ( Boot class loader ) (Bootstrap classLoader)
1. be responsible for Java Loading of core classes ,, As part of the virtual machine , such as system.string, stay JDK Of jre Of lib In the catalog rt.jar In file 
2: Extend the classloader (Extension classLoader)
2. be responsible for jre Under the extended directory of jar Package loading , stay JDK in jre Of lib In the catalog ext Catalog

3: system class loader (system classLoader)
1. Responsible for jvm Load at startup from Java Ordered class file , as well as classpath Specified by environment variable jar Path of packages and classes , It includes everything jdk Class provided (rt-jar). Including classes written by developers and third-party jar package
2: load classpath Next class
3: Also called Application ClassLoader ,sun.misc.Launcher$AppClassLoader, It is System.getClassLoader() The return value of
4: Delegation mechanism for class loader
Class loader relationship
1: The upper leadership of the system class loader -----------> Extend the classloader
2: The upper leadership of the extension class loader -----------> Root loader
The order of execution of the delegation mechanism
- Created an object User user=new User();
- The system class loader found new User This class
- The system will call the automatic leader , Let the extension go to its own site to load user class
- Expansion will call their leaders , Let the root class loader go to its own site to load user class
1: The root class loader goes jre Under the rt.jar Go looking for A class
- If you find it , Just load , Then return user Object's class Object to the extension class loader , The extended class loader will this user.class The file is returned to the system class loader
- If not
2: The root class loader returns a null, Expansion will look for user class
- If the extension class is found user class , Just load , return user Object's class File to system classloader
- If not
3: The extension class is returned to the system class loader null, The system goes to its own territory ( Applications ) load A class
- If you find it , load , Go back to this class file , end
- If not , Throw out classNotFoundException Class is exception not found

5: Parent delegation model
summary
The process of parental delegation model : If a class loader receives a class load request , First, I won't load it myself , Instead, the request is sent to its own parent loader to complete , This is true of class loaders at every level . So all the load requests should eventually be sent to the top-level boot loader . Only when the parent class feedbacks that it cannot complete the load request , The subclass loader will try to complete it by itself

2: Break the parental delegation model
1: Three large-scale destruction of the model
- JDK 1.0->1.2 , loadClass() -> findClass()
- Model defects :JNDI service ,SPI The extension class is implemented by the manufacturer , It is impossible to recognize these classes when starting class loading . We have to introduce Thread context Class loading Thread Context ClassLoader. This kind of loader can pass setContextClassLoader() Set up , If the thread was created without setting , Will inherit from the parent thread . If it is not set in the global scope of the application , The default is AppClassLoader. With this ,JNDI The service can load the required SPI Extension code , That is, the parent class loader asks the child class loader to complete the class loading action . This actually violates the general principle of the parental delegation model , But there's nothing to do .
- The pursuit of program dynamics : “ Hot replacement ” , OSGI. JSR-291
2: Develop your own class loader
Class loading security mechanism
- The loading of classes will be from the root class loader --- Extend the classloader --- The system class loader goes down layer by layer , There will be no repeated loading
- Write a class by yourself Java.lang.string, Want to replace Java Root class library
1 Will not load , Because there are Java.lang.string. Will not load the classes written by yourself
6: Class loaders and threads
- Each thread has a separate class loader and cpu register ( Record the working state of the thread )
- getContextClassLoader: Get the classloader of the current thread

边栏推荐
- Centos7 installs Damon stand-alone database
- Ansible installation and deployment of automated operation and maintenance
- The solution to the error of [installation detects that the primary IP address of the system is the address assigned by DHCP] when installing Oracle10g under win7
- 北京一卡通以35288.8529万元挂牌出让68.45%股权,溢价率为84%
- After reading this article, I found that my test cases were written in garbage
- Solve the problem of repeated clicking of button uibutton
- Notes on the use of IEEE transaction journal template
- On the number of solutions of indefinite equations
- 自动化渗透扫描工具
- Click event to create a new node
猜你喜欢

Not configured in app.json (uni releases wechat applet)

Rhcsa sixth note

Centos7安装达梦单机数据库
![[untitled]](/img/67/793d1fd7c295f0af9f683ffa389757.png)
[untitled]

Dameng real-time active and standby cluster construction

Unity 委托 (Delegate) 的简单理解以及实现

TypeError: Cannot read property ‘make‘ of undefined

Nessus security testing tool tutorial

Unity pedestrians walk randomly without collision

uni-app 背景音频 熄屏或者退回桌面之后不在播放
随机推荐
【C语言笔记分享】——动态内存管理malloc、free、calloc、realloc、柔性数组
小熊派 课程导读
Bibliometrix: dig out the one worth reading from thousands of papers!
Centos7 installs Damon stand-alone database
【NLP】下一站,Embodied AI
Stack and queue - 20. Valid parentheses
String - Sword finger offer 58 - ii Rotate string left
5年接触近百位老板,身为猎头的我,发现升职的秘密不过4个字
Clear all spaces in the string
The latest and complete Flink series tutorials in 2021_ Preliminary exploration of Flink principle and flow batch integration API (II. V) V2
binlog、iptables防止nmap扫描、xtrabackup全量+增量备份以及redlog和binlog两者的关系
Must use destructuring props assignmenteslint
数据湖系列文章
电赛设计报告模板及历年资源
CSDN垃圾的没有底线!
Rasa 3.x 学习系列-Rasa [3.2.4] - 2022-07-21 新版本发布
Regular expression and bypass cases
Some simple commands
How to quickly wrap lines in Excel table
JS judge whether the data is empty