VM class loading mechanism --- class loader

Class loader

  Class loading phase " for descriptions of such binary byte stream through a fully qualified class name of " this action to the outside to achieve the java virtual machine. To achieve this operation code module called "class loader" .

Class and class loader

  It is equal to two classes, the need class itself equal , and performed using a class loader to load the same , because each class loader has a separate class name space .

  Equal Here, the object class including Class equals () method , IsAssignableFrom () method , isInstance () method returns a value of true, also includes the use instanceof keyword object belongs do relationship determination result is true.

Classification class loader

From the perspective of the Java virtual machine is concerned, there are only two different class loader:

  • Start class loader (Bootstrap ClasssLoader) use c ++ realize, it is part of a virtual machine.
  • All other class loaders , implemented in Java, independent of the virtual machine, inherited from abstract class java.lang.ClassLoader.

From the perspective of Java developer's perspective, the class loader can be divided into more detail:

  • Boot loader class : This class loader will be stored in \ lib directory, or by the specified parameters -Xbootclasspath path, and a virtual machine identification (by file name recognition, such as rt.jar, even if the name does not meet the library only on the
    lib directory will not is loaded) libraries loaded into the virtual machine's memory. Start class loader can not be directly referenced Java program, users write custom class loader, if you need to load the delegates to start class loader, can be used directly instead of null.
  • Extension class loader (Extension ClassLoader): This class loader is achieved by ExtClassLoader (sun.misc.Launcher $ ExtClassLoader), which is responsible for All libraries / lib / ext or java.ext.dir system variables specified path loaded into memory, developers can be used directly extended class loader.
  • Application class loader : (Application ClassLoader) This class loader is achieved by AppClassLoader (sun.misc.Launcher $ AppClassLoader). Since the class loader is the ClassLoader getSystemClassLoader () Returns the value of the method, it is generally called the system class loader. It is responsible for loading the user's path specified by the library, developers can directly use this class loader, if the application is not too own custom class loader, in general, this program is the default class loader.

Parent delegation model

The figure that shows the hierarchical relationship between class loader , called the parent class loader delegation model.

  Parent delegation model in addition to the top of the boot class loader, the rest of the class loader has its own parent class loader . Here parent-child relationship between the class loader is generally not achieved to inheritance relationship, but the use of a combination of multiplexed code in relation to the parent loader.

  Parent delegation model of work process : If a class loader loads the class received a request, it first does not own to try to load this class, but to delegate this request to the parent class loader to complete each level class loader is true, therefore all class load request should eventually reached the top of the boot class loader only when the parent class loader to load the feedback they can not complete the request, the child will own class loader to load.

  Parents of the benefits of the delegation model : java class with its class loader together have a relationship with a priority level. So that the base class unified.

  Parents entrust implementation of the model : implementation code of the parent delegation model are concentrated in java.lang.ClassLoader among the loadClass method ().

  As shown in the following code: first check has been loaded before, if the call is not loaded loadClass () method of the parent class loader , when the loader is empty parent class is used by default as the parent class loader boot loader, if parent fails to load, after ClassNotFoundException exception is thrown, then call their findClass () method to load.

protected synchronized Class<?>loadClass(String name,boolean resolve)throws ClassNotFoundException{
    //首先检查请求的类是否已被加载过
    Class c=findLoadedClass(name);
    if(c==null){
        try{
            if(parent!=null){
                c=parent.loadClass(name,false);
            }else{
                c=findBootstrapClassOrNull(name);
            }
        }catch(ClassNotFoundException e){
            //如果父类加载器抛出ClassNotFoundException
            //说明父类加载器无法完成加载请求
        }
        if(c==null)
            c=findClass(name);
    }
    if(resolve)
        resolveClass(c);
    return c;
}

Guess you like

Origin www.cnblogs.com/yjxyy/p/11128769.html