java class loading mechanism parent delegate

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/shy415502155/article/details/88167713

Why do parents need to delegate model it?

A custom java.lang.Stringclass, the Stringclass with the system Stringas a function of the class, one of the functions only minor modifications. For example equalsfunction, which is often used, if you add some "virus code" which this function. And added to a custom class loader JVMin. At this point, if there is no parent delegation model, you JVMmight mistake the custom java.lang.Stringclass is a system of Stringclasses, resulting in "virus code" is executed. 

With parents delegation model, a custom java.lang.Stringclass will never be loaded into memory 

 Java is running on the Java Virtual Machine (JVM) in, but it is how it is run in the JVM of it? We prepared the IDE Java source code is compiled into compiled .class byte code file. Then we have the responsibility of ClassLoader will load the class into the JVM asking price of execution.

java class loader comes three

1. Start class loader (Bootstrap ClassLoader): implemented by the C ++ language (for HotSpot), is responsible for the store at <JAVA_HOME> \ lib directory or -Xbootclasspath parameter specifies the path to the library is loaded into memory.

2. Other class loader : implemented by the Java language, inherited from abstract class ClassLoader.

      ① extension class loader (Extension ClassLoader): responsible for loading the <JAVA_HOME> \ All library lib \ ext directory or system variable java.ext.dirs specified path.
      ② application class loader (Application ClassLoader). Responsible for loading the user class path (classpath) on the specified library, we can directly use this class loader. Generally, if we do not have custom class loaders default is to use the loader.

Parents delegate mechanism

In class loadClass method java.lang.ClassLoader

private final ClassLoader parent;
public Class<?> loadClass(String name) throws ClassNotFoundException {
        return loadClass(name, false);
    }

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;
        }
    }
protected Class<?> findClass(String name) throws ClassNotFoundException {
        throw new ClassNotFoundException(name);
    }

First calls itself findLoadedClass () to find this class in memory, if not found by AppClassLoader will call its parent class loader that is ExtClassLoader memory to find the same, that is, by then it will not be found if the parent class loader BootStrap to find,

If you have not found it will return one level, that is loaded by the load path BootStrap its own definition, if not it will be loaded by ExtClassLoader

According to its own definition of the load path to load, if the load will not be loaded by AppClassLoader. All the way to the bottom, if no loader can load, will throw ClassNotFoundException

Guess you like

Origin blog.csdn.net/shy415502155/article/details/88167713