ClassLoader类加载器(四):双亲委托机制

2020-12-12  本文已影响0人  bug喵喵

一. 双亲委派机制

当java应用程序需要加载类的时候,调用的ClassLoader首先会委托自己的父ClassLoader去加载类,如果父ClassLoader可以加载类,那么由父ClassLoader加载类,如果父ClassLoader不能加载类,那么才由当前调用的ClassLoader去加载类

1. ClassLoader委托链

image

1.1 Bootstrap ClassLoader(根加载器)

public class JVMTest {
    public static void main(String[] args) {
        //String.class的ClassLoader是Bootstrap ClassLoader
        System.out.println(String.class.getClassLoader());
    }
}

//运行结果:
    null

1.2 Extension ClassLoader(扩展加载器)

1.3 App ClassLoader(应用加载器)

//JVM测试类
public class JVMTest {
   public static void main(String[] args) {
       //JVMTest.class是在classpath下
       //由AppClassLoader加载
       System.out.println(JVMTest.class.getClassLoader());
   }
}

//运行结果:
   sun.misc.Launcher$AppClassLoader@73d16e93

2.ClassLoader初始化源码剖析

Launcher类是JVM的入口

public class Launcher {
    //bootstrap ClassLoader加载路径
    private static String bootClassPath = System.getProperty("sun.boot.class.path");

    //构造函数
    public Launcher() {
        //扩展ClassLoader 初始化
        Launcher.ExtClassLoader var1;
        try {
            var1 = Launcher.ExtClassLoader.getExtClassLoader();
        } catch (IOException var10) {
            throw new InternalError("Could not create extension class loader", var10);
        }

        try {
            //应用ClassLoader初始化
            this.loader = Launcher.AppClassLoader.getAppClassLoader(var1);
        } catch (IOException var9) {
            throw new InternalError("Could not create application class loader", var9);
        }

        Thread.currentThread().setContextClassLoader(this.loader);
        String var2 = System.getProperty("java.security.manager");
        if (var2 != null) {
            SecurityManager var3 = null;
            if (!"".equals(var2) && !"default".equals(var2)) {
                try {
                    var3 = (SecurityManager)this.loader.loadClass(var2).newInstance();
                } catch (IllegalAccessException var5) {
                } catch (InstantiationException var6) {
                } catch (ClassNotFoundException var7) {
                } catch (ClassCastException var8) {
                }
            } else {
                var3 = new SecurityManager();
            }

            if (var3 == null) {
                throw new InternalError("Could not create SecurityManager: " + var2);
            }

            System.setSecurityManager(var3);
        }

    }

    //bootstrap classloader并没有具体的实例,而是JVM实现的一部分
    private static class BootClassPathHolder {
        static final URLClassPath bcp;
    }

    //应用ClassLoader
    static class AppClassLoader extends URLClassLoader {
        //...
    }

    //扩展ClassLoader
    static class ExtClassLoader extends URLClassLoader {
        private static volatile Launcher.ExtClassLoader instance;
        //...
    }

}

3.ClassLoader双亲委托机制源码剖析

public abstract class ClassLoader {

    //父加载器
    private final ClassLoader parent;

    //当前ClassLoader加载的类列表
    private final Vector<Class<?>> classes = new Vector<>();

    //构造函数 
    private ClassLoader(Void unused, ClassLoader parent) {
        //父加载器赋值
        this.parent = parent;
        if (ParallelLoaders.isRegistered(this.getClass())) {
            //类已注册
            parallelLockMap = new ConcurrentHashMap<>();
            package2certs = new ConcurrentHashMap<>();
            domains =
                Collections.synchronizedSet(new HashSet<ProtectionDomain>());
            assertionLock = new Object();
        } else {
            parallelLockMap = null;
            package2certs = new Hashtable<>();
            domains = new HashSet<>();
            assertionLock = this;
        }
    }

    /**
     * 通过名称加载类
     * @param name 类全名
     */
    public Class<?> loadClass(String name) throws ClassNotFoundException {
        return loadClass(name, false);
    }

    /**
     * 通过名称加载类
     * @param name 类全名
     * @param resolve 是否解析
     */
    protected Class<?> loadClass(String name, boolean resolve)
        throws ClassNotFoundException
    {
        synchronized (getClassLoadingLock(name)) {
            //判断类是否被加载,最终调用的是native方法
            Class<?> c = findLoadedClass(name);
            if (c == null) {
                long t0 = System.nanoTime();
                try {
                    if (parent != null) {
                        //如果父ClassLoader不为null,
                        //先通过父ClassLoader加载类
                        c = parent.loadClass(name, false);
                    } else {
                        //如果父ClassLoader为null
                        //通过BootstrapClassloader来加载类
                        c = findBootstrapClassOrNull(name);
                    }
                } catch (ClassNotFoundException e) {

                }

                if (c == null) {
                    //如果父ClassLoader加载不到类
                    //那么当前ClassLoader继续加载
                    long t1 = System.nanoTime();
                    c = findClass(name);

                    //记录加载时间和数目
                   sun.misc.PerfCounter.getParentDelegationTime().addTime(t1 - t0);
                   sun.misc.PerfCounter.getFindClassTime().addElapsedTimeFrom(t1);
                    sun.misc.PerfCounter.getFindClasses().increment();
                }
            }
            if (resolve) {
                resolveClass(c);
            }
            return c;
        }
    }

    //通过类名获取锁
    protected Object getClassLoadingLock(String className) {
        Object lock = this;
        if (parallelLockMap != null) {
            Object newLock = new Object();
            lock = parallelLockMap.putIfAbsent(className, newLock);
            if (lock == null) {
                lock = newLock;
            }
        }
        return lock;
    }
}

4.双亲委托机制的优势

二.自定义ClassLoader

1. ClassLoader抽象类中中几个重要方法

//YGX自定义ClassLoader
public class YgxClassloader extends ClassLoader {
    /**
     * 查找并加载类
     * 
     * @param name 类全名
     */
    @Override
    protected Class<?> findClass(String name) throws ClassNotFoundException {
        byte[] data = loadData(name);
        return defineClass(name, data, 0, data.length);
    }

    // 获取数据
    private byte[] loadData(String name) {
        File file = new File("jvm" + File.separator + name + ".class");
        byte[] data = null;
        try {
            InputStream is = new FileInputStream(file);
            data = new byte[is.available()];
            is.read(data, 0, data.length);
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return data;
    }

    public static void main(String[] args) throws ClassNotFoundException {
        YgxClassloader ycl = new YgxClassloader();
        Class<?> clazz = ycl.loadClass("Hello");
        System.out.println(clazz.getName());
        System.out.println(clazz.getClassLoader());
    }

}

//运行结果:
    Hello
    jvm.YgxClassloader@15db9742

2. thread.getContextClassLoader()和class.getClassLoader()

上一篇下一篇

猜你喜欢

热点阅读