java类加载的类加载器

1、系统提供的类加载器有三种

1)启动类加载器(Bootstrap ClassLoader)在我们使用的hotspot版本java虚拟机中使用c++实现,无法被java程序员直接使用

2)扩展类加载器(Extention ClassLoader)放在<JAVA_HOME>\lib\ext目录下,用来加载扩展的类

3)应用程序类加载器(Application ClassLoader)负责加载用户类路径(classpath)指定的类,一般情况这个就是程序默认的类加载器




2、双亲委托机制:

以一种组合的关系类复用父类加载器,而不是继承,加载一个类之前会先递归调用的方式去找到父类加载器尝试加载,一旦父类加载器为null则使用启动类加载器去进行加载,所以每次都是先从启动类加载器开始加载,不行则抛出异常,都不能加载就自己进行加载。

 
 
 
 
public abstract class ClassLoader {
    ...
    //组合的方式包含了父类加载器
    private final ClassLoader parent;
    ...
//加载类的方法
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 {
                    //null的话表示该父类是bootstrap classloader
                    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);
                    sun.misc.PerfCounter.getFindClassTime().addElapsedTimeFrom(t1);
                    sun.misc.PerfCounter.getFindClasses().increment();
                }
            }
            if (resolve) {
                resolveClass(c);
            }
            return c;
        }
    }
}



猜你喜欢

转载自blog.csdn.net/qq_31489805/article/details/79689818