Learning class loading mechanism of 2_____ parent delegation model

     By learning before we learned the common three types of loader : BootstrapClassLoader, ExtClassLoader, APPClassLoader, and users can re-write findClass by inheriting the abstract class ClassLoader () from custom class loader. Then load the class, how to ensure global uniqueness class? ? ? We use the JVM parents agreed to appoint mechanisms loading mechanism class loader.

Parents delegation model:

     In the parent delegation model, in addition to the boot class loader (BootstrapClassLoader), each class has a super class loader loads, such as: AppClassLoader super class loader is ExtClassLoader, user-defined class loader is AppClassLoader. When actual class loader, the current class loader will not load classes directly, but would like to load delegate tasks to their super class loader, super class loader and then delegated to their super class loader, until it reaches the top boot class loader (BootstrapClassLoader), if the super class loader can not load the class assigned to it, it will be passed on to load the task will own the next stage loader (as shown above).

    Delegated advantage parents mechanism: can effectively ensure a globally unique class, when a plurality of the fully qualified name of the same type occur when a program (such as two categories called: com.user.Test), class loader performs load when, always will load only one of the classes, two classes will not perform the load.

    Parents delegate source model of reading:

protected Class loadClass (String var1, <?> boolean var2) throws ClassNotFoundException {
         the synchronized ( the this .getClassLoadingLock (var1)) {
             // first check has already been loaded over the goal before Type 
            Class var4 = the this .findLoadedClass (var1);
             IF (var4 == null ) {
                 Long var5 = to System.nanoTime (); 

                the try {
                     // if there is a superclass to be delegated to the superclass loading 
                    IF ( the this .parent =! null ) { 
                        var4 =the this .parent.loadClass (var1, to false ); 
                    } the else {
                        // if there is a superclass delegated directly to the boot to load class loader 
                        var4 = the this .findBootstrapClassOrNull (var1); 
                    } 
                } the catch (a ClassNotFoundException var10) {
                    // when the exception is thrown ClassNotFoundException described boot class loader fails 
                    ; 
                } 

                IF (var4 == null ) {
                 // if it exceeds tired loader can not be loaded. Self loading proceeds 
                    Long Var7 =  to System.nanoTime ();
                    var4 =this.findClass(var1);
                    PerfCounter.getParentDelegationTime().addTime(var7 - var5);
                    PerfCounter.getFindClassTime().addElapsedTimeFrom(var7);
                    PerfCounter.getFindClasses().increment();
                }
            }

            if (var2) {
                this.resolveClass(var4);
            }

            return var4;
        }
    }

Guess you like

Origin www.cnblogs.com/xbfchder/p/11412244.html