双亲委派模型
对于JVM类加载器来说,其实就是如下的code,JDK提供的三个类加载器,每个类加载器都加载自己范围内的类。Boot\EXT\APP 三个。双亲委派一句话就是,先让老爸处理,老爸处理不了,给爷爷。爷爷处理不了,自己在处理。
这样其实就会出现一个问题,越是基础的类,都有父类进行加载,出现两个java.lang.Object类,只会加载核心包的。不会加载用户自己的类。
protected Class<?> loadClass(String name, boolean resolve)
throws ClassNotFoundException
{
synchronized (getClassLoadingLock(name)) {
// First, check if the class has already been loaded
// 检查请求的类是否已经加载过了
Class<?> c = findLoadedClass(name);
// 如果加载过 跳过
// 没有加载过 执行
if (c == null) {
long t0 = System.nanoTime();
try {
// 父类不为空,先进行父类加载器加载
if (parent != null) {
c = parent.loadClass(name, false);
} else {
// 如果父加载器为空 使用跟加载器加载
c = findBootstrapClassOrNull(name);
}
} catch (ClassNotFoundException e) {
// ClassNotFoundException thrown if class not found
// from the non-null parent class loader
// 如果抛出异常,说明父类加载器无法加载,找不到
}
if (c == null) {
// 父类加载器无法加载的时候,调用本身的加载器进行加载
// If still not found, then invoke findClass in order
// to find the class.
long t1 = System.nanoTime();
c = findClass(name);
// this is the defining class loader; record the stats
sun.misc.PerfCounter.getParentDelegationTime().addTime(t1 - t0);