当前位置:网站首页>JVM内存模型:类加载器的分类和获取
JVM内存模型:类加载器的分类和获取
2022-07-22 03:39:00 【暮色_年华】
四者是包含关系,不是继承关系。
Bootstrap ClassLoader:启动类加载器,引导类加载器
*使用C/C++语言实现,嵌套在JVM内部。
*用来加载Java的核心库,用于提供提供JVM自身需要的类。
*没有父类加载器。
*加载扩展类和应用程序类加载器,并指定为它们的父类加载器。
*出于安全考虑,Bootstrap启动类加载器只加载包名为java、javax、sun等开头的类。
拓展类加载器(Extension ClassLoader)
*java语言编写,派生与ClassLoader类,父类加载器为BootStrap加载器。
*从JDK安装目录的拓展目录下加载类库。如果用户创建的JAR放在该目录下,也会自动由拓展类加载器加载。
应用程序类加载器(系统类加载器,AppClassLoader)
*java语言编写,派生与ClassLoader类,父类加载器为拓展类加载器。
*负责加载环境变量classpath或系统属性java.class.path指定路径下的类库
*是程序中默认的类加载器,一般来说,java应用的类都是由它完成加载。
获取ClassLoader的途径:
public class ClassLoaderTest {
public static void main(String[] args) {
//获取系统类加载器
ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();
System.out.println(systemClassLoader);//[email protected]
//获取其上层:扩展类加载器
ClassLoader extClassLoader = systemClassLoader.getParent();
System.out.println(extClassLoader);//[email protected]
//获取其上层:获取不到引导类加载器
ClassLoader bootstrapClassLoader = extClassLoader.getParent();
System.out.println(bootstrapClassLoader);//null
//对于用户自定义类来说:默认使用系统类加载器进行加载
ClassLoader classLoader = ClassLoaderTest.class.getClassLoader();
System.out.println(classLoader);//[email protected]
//String类使用引导类加载器进行加载的。---> Java的核心类库都是使用引导类加载器进行加载的。
ClassLoader classLoader1 = String.class.getClassLoader();
System.out.println(classLoader1);//null
}
}
public class ClassLoaderTest2 {
public static void main(String[] args) {
try {
//1.Class.getClassLoader
ClassLoader classLoader = Class.forName("java.lang.String").getClassLoader();
System.out.println(classLoader);
//2.线程上下文
ClassLoader classLoader1 = Thread.currentThread().getContextClassLoader();
System.out.println(classLoader1);
//3.系统加载器
ClassLoader classLoader2 = ClassLoader.getSystemClassLoader().getParent();
System.out.println(classLoader2);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
边栏推荐
- 这个好用的办公网优化工具,官宣免费了
- [learn rust together] rust preparation before learning -- Annotation and formatted output
- Nftscan and port3 have reached strategic cooperation in the field of NFT data
- SwinIR: Image Restoration Using Swin Transformer论文阅读
- 万亿市值公链竞争白热化,新公链还有机会吗?
- Word: insert vector diagram with specified color
- QT笔记——自定义的QListWidget
- Scala variables and data types (2)
- 【leetcode】
- 关于引体向上器和腰垫的选购说明
猜你喜欢
Helm理解和使用
Soc之按键控制LED
命令行程序测试自动化
Helm understanding and use
DOM style operation
BookPageCnt
Albumin nanoparticles / gossypol albumin nanoparticles / lapatinib albumin nanoparticles coated with DNA and photosensitizer CE6
线段树学习笔记
Advertising is everywhere. How can we use advertising to promote our products?
Online RPC timeout troubleshooting and subsequent GC tuning ideas
随机推荐
How does the 2022 social e-commerce model split marketing? -- Share purchase
QT笔记——自定义的QListWidget
Mnemonic search
M using similink simulation module to realize dynamic simulation of multipath channel
Metauniverse enabling scenario Finance: a new track for the competitive development of commercial banks
Understand the three mountains of JS
P6327 区间加区间sin和
The principle of distributed transaction is simple, and it is full of holes
Learning notes of line segment tree
万亿市值公链竞争白热化,新公链还有机会吗?
对接三方支付接口需要满足哪些需求?
Codeforces Round #719 (Div. 3)
PX4模块设计之十一:Built-In框架
MySQL advanced addition, deletion, modification and query operations, including detailed explanation and operation of various queries and table design explanation
命令行程序测试自动化
Use of yarn
Discrete problem sorting
QT笔记——QTimer 和 QTimerEvent简单介绍
QT notes - customized qlistwidget
Is it true that microservices do not choose databases? How to choose?