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

java

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

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

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

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

/**

* 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;

}

}

双亲委派的好处: 

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

破坏双亲委派的模型: 

参考:

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

[2] tomcat类加载器

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

[4] 深入理解 Tomcat(四)Tomcat 类加载器之为何违背双亲委派模型 https://blog.csdn.net/qq_38182963/article/details/78660779

以上是 【深入Java虚拟机】二 类加载与双亲委派 的全部内容, 来源链接: utcz.com/z/394386.html

回到顶部