美文网首页
双亲委派模型源码

双亲委派模型源码

作者: 火乐君_52cd | 来源:发表于2020-03-28 15:50 被阅读0次
    private final ClassLoader parent; 
    protected Class<?> loadClass(String name, boolean resolve)
            throws ClassNotFoundException
        {
            synchronized (getClassLoadingLock(name)) {
                // 首先,检查请求的类是否已经被加载过
                Class<?> c = findLoadedClass(name);
                if (c == null) {
                    long t0 = System.nanoTime();
                    try {
                        if (parent != null) {//父加载器不为空,调用父加载器loadClass()方法处理
                            c = parent.loadClass(name, false);
                        } else {//父加载器为空,使用启动类加载器 BootstrapClassLoader 加载
                            c = findBootstrapClassOrNull(name);
                        }
                    } catch (ClassNotFoundException e) {
                       //抛出异常说明父类加载器无法完成加载请求
                    }
    
                    if (c == null) {
                        long t1 = System.nanoTime();
                        //自己尝试加载
                        c = findClass(name);
    
                        // this is the defining class loader; record the stats
                        sun.misc.PerfCounter.getParentDelegationTime().addTime(t1 - t0);
                        sun.misc.PerfCounter.getFindClassTime().addElapsedTimeFrom(t1);
                        sun.misc.PerfCounter.getFindClasses().increment();
                    }
                }
                if (resolve) {
                    resolveClass(c);
                }
                return c;
            }
        }
    

    相关文章

      网友评论

          本文标题:双亲委派模型源码

          本文链接:https://www.haomeiwen.com/subject/otqsuhtx.html