ClassLoader-1

作者: 01010100 | 来源:发表于2018-02-27 19:27 被阅读21次

    类加载过程:

    类从被加载到虚拟机内存中,到从内存卸载,生命周期:加载 -> 连接 -> 初始化 -> 使用 -> 卸载。其中,连接又分3个部分:验证、准备、解析。

    加载:加载字节码等。验证:验证合法性等。

    准备:为类变量分配内存并设置类变量(不包括实例变量)初始值,这些变量所使用的内存都在方法区分配。 如下示例,此阶段只设置初始值,即此时value=0。而特殊情况,VALUE_C=123,因为VALUE_C是常量,常量在这个阶段会被初始化成123。class A { puclic static int value = 123; public static final int VALUE_C = 123;}

    解析:解析阶段是虚拟机将常量池内的符号引用替换为直接引用的过程。

    判断类是否相等:对于任意一个类,都需要由他的类加载器和类本身一同确立起在虚拟机中的唯一性。即比较两个类是否“相等”,只有在同一个类加载器加载的前提下才有意义,否则,即使来源同一个类,被不同的类加载器加载,那这两个类必定不相等。

    双亲委派机制:

    TheClassLoaderclass uses a delegation model to search forclasses and resources.  Each instance ofClassLoaderhas anassociated parent class loader.  When requested to find a class orresource, aClassLoaderinstance will delegate the search for theclass or resource to its parent class loader before attempting to find theclass or resource itself.  The virtual machine's built-in class loader,called the "bootstrap class loader", does not itself have a parent but mayserve as the parent of aClassLoaderinstance. 

    从以上描述中,我们可以总结出如下四点:

    1、类的加载过程采用委托模式实现

    2、每个 ClassLoader 都有一个父加载器。

    3、类加载器在加载类之前会先递归的去尝试使用父加载器加载。

    4、虚拟机有一个内建的启动类加载器(bootstrap ClassLoader),该加载器没有父加载器,可以作为其他加载器的父加载器。

    1、Bootstrap ClassLoader启动类加载器,C++实现,加载/lib 目录中的文件,并且该类加载器只加载特定名称的文件(如 rt.jar),而不是该目录下所有的文件。

    2、ExtClassLoader,负责加载\lib\ext目录中或系统变量java.ext.dirs 所指定的目录中的文件。

    3、AppClassLoader,负责加载用户类路径中的文件java.class.path。

    相关文章

      网友评论

        本文标题:ClassLoader-1

        本文链接:https://www.haomeiwen.com/subject/nzwmxftx.html