In-depth understanding of JVM (a) class loader part: parents delegation model

Father commissioned the class loader mechanism

  • Father delegation mechanism, each class loader in accordance with parent-child relationships forming a tree structure, except the root class loader, the rest of the class loader has one and only one parent loader.

    Let the top level can be added to the parent plant plus loader (all loaded loader, a loader that is at the top level of the load, if can not be loaded, the failure)

-Bootstrap ClassLoader class loader boot

$ JAVA_HOME in jre / lib / rt.jar in every class, ++ implemented by c, is not a subclass of ClassLoader

-Extension ClassLoader extension classloader

Responsible for loading the java platform expansion pack features some of the jar, including the jar package / * under $ JAVA_HOME in the jre / lib jar or -Djava.ext.dirs specified directory

  • App ClassLoader system class loader
  • jar package is responsible for loading the classpath and directory specified in the class

Successfully loaded the class loader is called custom class loader

All object references can be successfully returned Clas class loader is called initial class loader (including definitions tired loader)

eg1:
public class MyTest7 {
    public static void main(String[] args)throws Exception {
        Class<?> clazz=Class.forName("java.lang.String");
        System.out.println(clazz.getClassLoader());

        Class<?> clazzc=Class.forName("com.aaa.test.c");
        System.out.println(clazzc.getClassLoader());
    }
}
class c{

}
//输出结果:
null
sun.misc.Launcher$AppClassLoader@18b4aac2

Different roles and class loader loader motion analysis

  • Get the parent of a class loader
public class MyTest13 {
    public static void main(String[] args) {
        ClassLoader classLoader=ClassLoader.getSystemClassLoader();
        System.out.println(classLoader);
        while(classLoader!=null){
            classLoader=classLoader.getParent();
            System.out.println(classLoader);
        }
    }
}

//输出结果:
sun.misc.Launcher$AppClassLoader@18b4aac2
sun.misc.Launcher$ExtClassLoader@512ddf17
null
    
  • Access to resources path
/**
 * @author lillcol
 * 2019/5/22-17:09
 */
public class MyTEST14 {
    public static void main(String[] args) throws IOException {
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        System.out.println(classLoader);

        String resourceName = "com/aaa/test/MyTest13.class";
        Enumeration<URL> urls = classLoader.getResources(resourceName);
        while (urls.hasMoreElements()) {
            URL url = urls.nextElement();
            System.out.println(url);
        }
    }
}

//输出结果:
sun.misc.Launcher$AppClassLoader@18b4aac2
file:/E:/IdeaProjects/AAAE2E2/target/scala-2.11/classes/com/aaa/test/MyTest13.class
  • Several methods of obtaining loader
/**
 * @author lillcol
 * 2019/5/22-17:09
 */
public class MyTEST14 {
    public static void main(String[] args) throws IOException, ClassNotFoundException {

        Class<?> clazz=Class.forName("com.aaa.test.MyTEST14");
        ClassLoader classLoader1 = clazz.getClassLoader(); //获得当前类的ClassLoader
        ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); //获得当前线程上下文的ClassLoader
        ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader(); //获得系统的ClassLoader
        System.out.println(classLoader1);
        System.out.println(contextClassLoader);
        System.out.println(systemClassLoader);
    }
}
//输出结果:
sun.misc.Launcher$AppClassLoader@18b4aac2
sun.misc.Launcher$AppClassLoader@18b4aac2
sun.misc.Launcher$AppClassLoader@18b4aac2

User-defined classes, loaded by the boot class AppClassLoader

String class loader to load the root class

/**
 * @author lillcol
 * 2019/5/22-17:09
 */
public class MyTEST14 {
    public static void main(String[] args) throws IOException, ClassNotFoundException {

        Class<?> clazz=MyTEST14.class;
        System.out.println(clazz.getClassLoader());
        System.out.println("----------");
        Class<?> clazz1=String.class;
        System.out.println(clazz1.getClassLoader());
    }
}
//输出结果:
sun.misc.Launcher$AppClassLoader@18b4aac2
----------
null

This article Zhang Long Learning Teacher in-depth understanding and experience notes the JVM, please indicate the source! ! !

Guess you like

Origin www.cnblogs.com/lillcol/p/11129932.html