JVM体系结构
类加载器及双亲委派机制
类加载器
JVM提供了三层类加载器:
- Bootstrap classLoader(启动类/根加载器):主要负责加载核心类库(如java.lang.*等),构造ExtClassLoader和AppClassLoader。
- ExtClassLoader(扩展类加载器):主要负责加载jre/lib/ext目录下的一些扩展类
- AppClassLoader(应用程序加载器):主要负责加载应用程序的主函数类
双亲委派机制
当某个类加载器需要加载某个.class
文件时,它首先把这个任务委托给他的上级类加载器,递归这个操作,如果上级的类加载器没有加载,自己才会去加载这个类。
类加载器级别:App->Ext>Boot
protected Class<?> loadClass(String name, boolean resolve)
throws ClassNotFoundException
{
synchronized (getClassLoadingLock(name)) {
// 首先检查这个classsh是否已经加载过了
Class<?> c = findLoadedClass(name);
if (c == null) {
long t0 = System.nanoTime();
try {
// c==null表示没有加载,如果有父类的加载器则让父类加载器加载
if (parent != null)