当前位置:网站首页>关于双亲委派机制和类加载的过程
关于双亲委派机制和类加载的过程
2022-07-06 09:20:00 【快醒醒鸭今天你编程了吗?】
我们写的java文件到最终运行,他必须经过编译和类加载两个阶段,而编译阶段的过程就是把
.java文件编译成.class文件,而类加载的阶段就是把.class文件加载到JVM内存里面,加载完毕
会得到一个Class对象,我们可以使用new关键字来实例化这个对象,如下图:

而类的加载过程需要涉及
到类加载器,JVM在运行的时候会产生三个类加载器,这三个类加载器组成了一个层级关系,每一个加载器分别去加载不同作用范围的jar包,比如说Bootstrap Classloader(启动类加载器)
它主要是负责加载java核心类库的加载也就是lib目录下rt.jar和resources.jar等,Extension ClassLoader(扩展类加载器)主要加载lib\ext目录下的一个jar包和class文件,Application ClassLoader(应用类加载器),主要负责当前应用里面classpath下面的所有jar包和class文件,处理系统自己提供的类加载器以外还可以通过ClassLoader类来实现自定义加载器,去满足一些场景需要。

而所谓的双亲委派模型,就是按照类加载器的层级关系,逐层进行委派,比如当我们需要加载一个class文件的时候,首先会去把这个class文件的查询和加载委派给父加载器去执行,如果父加载器无法加载,再尝试自己去加载这个class

我认为这样的好处:
- 安全性:因为这种层级关系实际上代表的是一种优先级,也就是所有的类加载优先要给到启动类加载器,那么对于核心类库中的一些类就没有办法被破坏,比如自己写一个java.lang.String最终还是要交给启动类加载器,自己写的java.lang.String就没办法去覆盖类库中的类。
- 我认为这种层级关系的设计,可以避免重复加载导致程序混乱的一些问题,如果父加载器已经加载过了,那么子加载器就没有必要再去加载了。

而运行结果显示:

边栏推荐
- Introduction pointer notes
- 1.C语言矩阵加减法
- Questions and answers of "basic experiment" in the first semester of the 22nd academic year of Xi'an University of Electronic Science and technology
- 最新坦克大战2022-全程开发笔记-2
- Introduction and use of redis
- Tyut Taiyuan University of technology 2022 introduction to software engineering summary
- MySQL limit x, -1 doesn't work, -1 does not work, and an error is reported
- 3.C语言用代数余子式计算行列式
- Rich Shenzhen people and renting Shenzhen people
- Small exercise of library management system
猜你喜欢
随机推荐
1. C language matrix addition and subtraction method
TYUT太原理工大学2022数据库大题之E-R图转关系模式
Interview Essentials: talk about the various implementations of distributed locks!
MPLS experiment
ROS machine voice
Tyut outline of 2022 database examination of Taiyuan University of Technology
Application architecture of large live broadcast platform
MySQL limit x, -1 doesn't work, -1 does not work, and an error is reported
6.函数的递归
(超详细二)onenet数据可视化详解,如何用截取数据流绘图
4.30动态内存分配笔记
用栈实现队列
更改VS主题及设置背景图片
12 excel charts and arrays
Inheritance and polymorphism (Part 2)
View UI plus released version 1.3.0, adding space and $imagepreview components
MySQL Database Constraints
Arduino+ds18b20 temperature sensor (buzzer alarm) +lcd1602 display (IIC drive)
System design learning (I) design pastebin com (or Bit.ly)
Cloud native trend in 2022









