The foreword 0.

Although two classes come from the same Class file and are loaded by the same virtual machine, they are not equal if they are loaded by different classloaders

1. Parental delegation model

1.1 Three types of loaders provided by the system

  • Bootstrap ClassLoader: load%JRE_HOME%\libDirectory classes, such as java.lang.*
  • Extension ClassLoader: Load%JRE_HOME%\lib\extClasses in the directory
  • Application ClassLoader: loads classes from the user’s classpath

You can add custom class loaders if necessary

1.2 Working Process

A class loader received the request, the class loader will delegate the request to the parent class loader, each level of the class loader, so the final request to start the class loader, only when the parent class loader said he was unable to complete the class load (in the scope of its search didn’t find the required class), the loader will try to load

1.3 Source Code Analysis

// java.lang.ClassLoader
protectedClass<? > loadClass(String name,boolean resolve)
    throws ClassNotFoundException
{
    synchronized (getClassLoadingLock(name)) {
        // First, check if the class has already been loaded
        // First, check whether the class is loaded
        Class c = findLoadedClass(name);
        if (c == null) {
            long t0 = System.nanoTime();
            try {
                if(parent ! =null) {
                    // Let the parent loader load
                    c = parent.loadClass(name, false);
                } else {
                    // The parent loader does not exist, indicating that the startup class loader is loaded by the startup class loaderc = 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();
                // If the parent class loader cannot be loaded, load it by itself
                c = findClass(name);

                // this is the defining class loader; record the statssun.misc.PerfCounter.getParentDelegationTime().addTime(t1 - t0); sun.misc.PerfCounter.getFindClassTime().addElapsedTimeFrom(t1); sun.misc.PerfCounter.getFindClasses().increment(); }}if (resolve) {
            resolveClass(c);
        }
        returnc; }}Copy the code

1.4 the flow chart

1.5 role

  • Avoid class reloading
  • Ensure that the base class is the same in all environments to avoid security problems when you customize overwrite the base class

Break the parental delegation model

  • Spis (JNDI, JDBC, JCE, JAXB, JBI, etc.) are basically loaded using Thread Context ClassLoader, which means that the parent ClassLoader loads classes through the subclass loader, breaking the parent delegate model
  • Code Hot swap, module Hot Deployment, e.g. OSGI

From Understanding the Java Virtual Machine in Depth (2nd edition)