【深刻Java虚拟机】二 类加载与双亲委派

https://blog.csdn.net/zhangliangzi/article/details/51338291  -参考html

 

双亲委派过程:当一个类加载器收到类加载任务时,当即将任务委派给它的父类加载器去执行,直至委派给最顶层的启动类加载器为止。若是父类加载器没法加载委派给它的类时,将类加载任务退回给它的下一级加载器去执行;除了启动类加载器之外,每一个类加载器拥有一个父类加载器,用户的自定义类加载器的父类加载器是AppClassLoader;双亲委派模型能够保证全限名指定的类,只被加载一次;双亲委派模型不具备强制性约束,是Java设计者推荐的类加载器实现方式;java

image.png 

双亲委派模型的源码实现:api

主要体如今ClassLoader的loadClass()方法中,思路很简单:先检查是否已经被加载过,若没有加载则调用父类加载器的loadClass()方法,若父类加载器为空则默认使用启动类加载器做为父类加载器。若是父类加载器加载失败,抛出ClassNotFoundException异常后,调用本身的findClass()方法进行加载。tomcat

 

/**
     * Loads the class with the specified <a href="#name">binary name</a>.  The
     * default implementation of this method searches for classes in the
     * following order:
     *
     * <ol>
     *
     *   <li><p> Invoke {@link #findLoadedClass(String)} to check if the class
     *   has already been loaded.  </p></li>
     *
     *   <li><p> Invoke the {@link #loadClass(String) <tt>loadClass</tt>} method
     *   on the parent class loader.  If the parent is <tt>null</tt> the class
     *   loader built-in to the virtual machine is used, instead.  </p></li>
     *
     *   <li><p> Invoke the {@link #findClass(String)} method to find the
     *   class.  </p></li>
     *
     * </ol>
     *
     * <p> If the class was found using the above steps, and the
     * <tt>resolve</tt> flag is true, this method will then invoke the {@link
     * #resolveClass(Class)} method on the resulting <tt>Class</tt> object.
     *
     * <p> Subclasses of <tt>ClassLoader</tt> are encouraged to override {@link
     * #findClass(String)}, rather than this method.  </p>
     *
     * <p> Unless overridden, this method synchronizes on the result of
     * {@link #getClassLoadingLock <tt>getClassLoadingLock</tt>} method
     * during the entire class loading process.
     *
     * @param  name
     *         The <a href="#name">binary name</a> of the class
     *
     * @param  resolve
     *         If <tt>true</tt> then resolve the class
     *
     * @return  The resulting <tt>Class</tt> object
     *
     * @throws  ClassNotFoundException
     *          If the class could not be found
     */
    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);
                    sun.misc.PerfCounter.getFindClassTime().addElapsedTimeFrom(t1);
                    sun.misc.PerfCounter.getFindClasses().increment();
                }
            }
            if (resolve) {
                resolveClass(c);
            }
            return c;
        }
    }

双亲委派的好处: less

    使用双亲委派模型来组织类加载器之间的关系,有一个很明显的好处,就是Java类随着它的类加载器(说白了,就是它所在的目录)一块儿具有了一种带有优先级的层次关系,这对于保证Java程序的稳定运做很重要。例如,类java.lang.Object类存放在JDK\jre\lib下的rt.jar之中,所以不管是哪一个类加载器要加载此类,最终都会委派给启动类加载器进行加载,这边保证了Object类在程序中的各类类加载器中都是同一个类。ide

破坏双亲委派的模型: ui

 

 

 

参考:this

[1] 《Java虚拟机精讲》[2] 《深刻理解Java虚拟机 JVM高级特性与最佳实践》[3]   http://blog.csdn.net/zhangliangzi/article/details/51338291spa

[2] tomcat类加载器.net

[3] 深刻探讨 Java 类加载器:https://www.ibm.com/developerworks/cn/java/j-lo-classloader/index.html

[4] 深刻理解 Tomcat(四)Tomcat 类加载器之为什么违背双亲委派模型 http://www.javashuo.com/article/p-nqgssvfe-d.html

相关文章
相关标签/搜索