zoukankan      html  css  js  c++  java
  • 类加载机制-双亲委派机制(三)

    1.JDK内置

     

    除了根类加载器其他的类只有1个父加载器

    通俗的说双亲委派机制:

    某一个类加载器想要加载特定的类,并不是由自己立刻去加载,而是把他委托给父加载器完成,如果父加载器上面还有父加载器,再委托给父亲的父亲,一直往上不断的追溯,直到根类加载器,由根类加载器特定的类,如果根类加载器无法加载,就把流程往下返回给拓展加载器,一直往下。在这个过程中只要有一个类加载器加载成功,就返回成功

    根类加载器:JRElib t.jar或者-Xbootclasspath选项指定的jar

    拓展类加载器:JRElibext*.jar-Djava.ext.dirs指定目录下的jar

    系统类加载器:CLASSPATHDjava.class.path所指定的目录下的类和jar

    自定义类加载器:java.lang.ClassLoader的子类自定义加载class

     2.JDK中获取类加载器

     

         * Returns the class loader for the class.  Some implementations may use
         * null to represent the bootstrap class loader. This method will return
         * null in such implementations if this class was loaded by the bootstrap
         * class loader.
    
      public class Test3 {
          public static void main(String[] args) throws ClassNotFoundException {
              Class<?> clazz = Class.forName("java.lang.String");
              System.out.println(clazz.getClassLoader());
          }
      }
    
      class C{
        
      }

    console:

    Null
    View Code

    原因:返回时null,说明String的类加载器是根类加载器

    public class Test3 {
        public static void main(String[] args) throws ClassNotFoundException {
            Class<?> clazz = Class.forName("java.lang.String");
            System.out.println(clazz.getClassLoader());
            
            Class<?> clazz2 = Class.forName("com.cn.test.Test3");
            System.out.println(clazz2.getClassLoader());
        }
    }
    
    class C{
        
    }

    console:

    null
    sun.misc.Launcher$AppClassLoader@73d16e93
    View Code
  • 相关阅读:
    解决:Android 8.0检测不到当前的activity
    flask学习(十三):过滤器
    打开相册上传图片
    完整的项目
    解决ScrollView滑动RecyclerView的卡顿
    RxJava
    CoordinatorLayout
    NestedScrollView,RecyclerView
    ViewPageIndicator
    RxJava的实现原理
  • 原文地址:https://www.cnblogs.com/xhlwjy/p/11290931.html
Copyright © 2011-2022 走看看