[deep understanding of Java virtual machine] class loader and parental delegation model

Class loader

For any class, the class loader that loads it and the class itself must jointly establish its uniqueness in the Java virtual machine. Each class loader has an independent class namespace. To put it simply: comparing whether two classes are equal makes sense only if they are loaded by the same class loader. As long as their class loaders are different, the two classes must be different.

There are three important classloaders built into the JVM. Except bootstrap classloader, other class loaders are implemented by Java and all inherit from Java lang.ClassLoader:

Start the class loader bootstrap classloader

C + + language implementation is a part of the virtual machine itself. Other class loaders exist independently outside the virtual machine and all inherit from abstract classes

java. lang.ClassLoader。

Responsible for loading < Java_ Home > \ lib directory, or the class in the path specified by the - xbootclasspath parameter and recognized by the virtual machine.

[identify by file name, such as rt.jar and tools.jar. Class libraries with inconsistent names will not be loaded even if they are placed in the Lib directory].

Extension classloader

Responsible for loading < Java_ Home > \ lib \ ext directory, or by Java All class libraries in the path specified by the ext.dirs system variable.

Application classloader

It is responsible for loading all class libraries on the user's classpath.

If there is no custom class loader in the application, it defaults to this class loader.

Parents Delegation Model

Requirements of parental delegation model

In addition to the top-level startup class loader, other class loaders should have their own parent class loader, but the parent-child relationship here is generally not implemented by inheritance, but by composition to reuse the code of the parent loader.

Working process of parent delegation model

Advantages of parental delegation model

Code implementation of parental delegation

protected Class<?> loadClass(String name,boolean resolve)
    throws ClassNotFoundException
{
    synchronized (getClassLoadingLock(name)) {
        // 首先,检查请求的类是否已经被加载过了
        Class<?> c = findLoadedClass(name);
        if (c == null) {
            long t0 = System.nanoTime();
            try {
                //不断调用父加载器的loadClass方法,直到BootstrapClassLoader
                if (parent != null) {
                    c = parent.loadClass(name,false);
                } else {
                    c = findBootstrapClassOrNull(name);
                }
            } catch (ClassNotFoundException e) {
                // 如果父类加载器抛出ClassNotFoundException说明父类加载器无法完成加载请求
            }

            if (c == null) {
                // 在父类加载器无法加载时 再调用本身的findClass方法来进行类加载
                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;
    }
}

reference resources:

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>