ClassLoader class loader

1. The action class loader

    ① .class file is loaded into the jvm ② the class file format requirements of the Uniform Code to jvm  

2. Class loading process

                

  Initialization timing:

      Examples a) class is created

      b) the class reflects call if the class did not need to initialize the triggers its initialization

      c) initialize a class, if the parent class is not initialized, you need to initialize the parent class

      d) when the virtual machine is started, the user needs to perform a main class, the main class is initialized

  VS class initialization class is instantiated

      Class to instantiate an object is to create an instance of a class

      Class initialization process is assigned an initial value of the member variables (static variables modified) class

3. The class loading mechanism

                  

    Start class (bootstrap classLoader): under jre / bin directory jar correspond to development projects JRE System

       Extension class (extension classLoader): Under jre / bin / ext directory corresponding to jar development project JRE System

    Application class (application classLoader): coders coding their own directory, the corresponding development projects

 

4, ClassLoader class loading principle (parents delegate)

  In addition to the boot class loader (bootstrap classloader) has a parent class loader. When a classloader to load a class instance, the task will be delegated to the parent class loader, if the parent class loader fails to load commissioned to start the class loader, if all else fails in the class will be the class loader loader . Finally, if none is loaded into this category, then throw classnotfoundexception

 

5, parents delegate advantage

  ① avoid repeated loading, after loading the parent class, do not need to be loaded once ClassLoader

  ② security reasons, parents commissioned by the way, you can protect yourself from random String defined instead of java core api, because String already was loaded by the bootstrap class loader (Bootstrcp ClassLoader) at startup, so a user-defined ClassLoader never I wrote a load of String

 

6, classLoader Code

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); //Recursive method, to have been looking at the root of the parent loader 
                    } the else { 
                        C = findBootstrapClassOrNull (name); 
                    } 
                } the catch (a ClassNotFoundException E) {
                     // a ClassNotFoundException thrown IF class Not found
                     // from The non-null parent class Loader 
                } 

                IF (C == null ) {
                     // the If Not found Still, in the findClass Order Invoke the then
                     // to Find The class. 
                    Long T1 =  to 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 final Class<?> findLoadedClass(String name) {
        if (!checkName(name))
            return null;
        return findLoadedClass0(name);
    }
View Code

    Code flow:  

           

 

    Note Point Code: ①synchronized (getClassLoadingLock (name))  

          分析getClassLoadingLock():

            a: getclassLoadingLock()方法:             

                            
protected Object getClassLoadingLock(String className) {
        Object lock = this;
        if (parallelLockMap != null) {
            Object newLock = new Object();
            lock = parallelLockMap.putIfAbsent(className, newLock);
            if (lock == null) {
                lock = newLock;
            }
        }
        return lock;
    }
View Code

            b: parallelLockMap定义:           

                private final ConcurrentHashMap<String, Object> parallelLockMap;
        parallelLockMap初始化在classLoader中:        
                            
private ClassLoader(Void unused, ClassLoader parent) {
        this.parent = parent;
        if (ParallelLoaders.isRegistered(this.getClass())) {
            parallelLockMap = new ConcurrentHashMap<>();
            package2certs = new ConcurrentHashMap<>();
            domains =
                Collections.synchronizedSet(new HashSet<ProtectionDomain>());
            assertionLock = new Object();
        } else {
            // no finer-grained lock; lock on the classloader instance
            parallelLockMap = null;
            package2certs = new Hashtable<>();
            domains = new HashSet<>();
            assertionLock = this;
        }
    }
View Code

            c: 根据ParallelLoadersRegistered状态的不同来给parallelLockMap 赋值

       ② findLoadedClass(name) 首先从jvm内存中查找该类,如果不存在再去加载

       ③ parent.loadClass(name, false);多次递归调用该方法,用以发现根节点的父类加载器

 

Guess you like

Origin www.cnblogs.com/enhance/p/10986340.html