美文网首页
理解ClassLoader.java

理解ClassLoader.java

作者: 迷途的探险家 | 来源:发表于2019-07-12 00:16 被阅读0次

        本节重点针对Java的ClassLoader的部分源码以及注释进行解读。我相信很多时候,我们没必要去速度获取别人给你的答案,通过自己去阅读官方的诠释,将会更有底气。 

1、ClassLoader是什么?


       A class loader is an object that is responsible for loading classes. The class ClassLoader is an abstract class. Given the binary name of a class, a class loader should attempt to locate or generate data that constitutes a definition for the class. A typical strategy is to transform the name into a file name and then read a "class file" of that name from a file system.

        翻开ClassLoader的源代码,查看类注释,你会发现第一段就是对于Classloader的解释,大致意思是:ClassLoader实例是用于加载 Class的,根据类的 binary name 获取到该类的结构定义(其实就是Class结构,这里的 binary name 指的是:javax.swing.JSpinner$DefaultEditor、java.lang.String等)。常见的策略是根据binary name获取到 .class 文件,根据文件系统进行类加载。

2、获取ClassLoader


Every Class object contains a reference to the ClassLoader that defined it.

参考Class.getClassLoader()的注释:

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. If this object represents a primitive type or void, null is returned. 

       这段话也是很直白,所有Class对象都会持有一个定义当前Class的ClassLoader的引用。当然也有返回null的两种情况,1、通过Bootstrap类加载器进行加载的类;2、原始类型(int, long等)、void。但我们在编写代码中原始类型是没有函数概念,我认为是在JVM层面,原始类型和void不属于Class的范畴,自然也没所谓加载器了。

发散一下:

        1、ClassLoader是一个抽象类,那么返回的加载器,应该是可以自定义的?

        2、Bootstrap 是什么?

        3、加载的时机

3、数组类的加载器


Class objects for array classes are not created by class loaders, but are created automatically as required by the Java runtime. The class loader for an array class, as returned by Class.getClassLoader() is the same as the class loader for its element type; if the element type is a primitive type, then the array class has no class loader.

        Java对象概念中,存在引用类型,原始类型。而引用类型会有一个数组类型特殊存在。以上是数组对象的Class加载器的说明,如:T[ ],那么Class加载器则是元素类型T所属的Class加载器,如果是原始类型数组,如: int[ ],那么是没有加载器的,这里的没有加载器我倾向是上面提到的原始类型同样的含义。

4、加载模式


The ClassLoader class uses a delegation model to search for classes and resources. Each instance of ClassLoader has an associated parent class loader. When requested to find a class or resource, a ClassLoader instance will delegate the search for the class or resource to its parent class loader before attempting to find the class or resource itself. The virtual machine's built-in class loader, called the "bootstrap class loader", does not itself have a parent but may serve as the parent of a ClassLoader instance.

        Class加载器是通过委托模式检索类与资源,每个Class加载器对象都持有父类加载器。JVM内置了一个名为 bootstrap class loader 的root级别的类加载器。

具体的加载逻辑:

        1、委托父类加载器进行类和资源的检索(检索成功即发起加载,父类也会先委托上一级父类发起检索,直到 bootstrap class loader)

        2、如果祖先加载器检索未成功,则自己处理。 

委托模式实现逻辑

网上有个大众的叫法:双亲模式、双亲委托、双亲等等。其实在JDK自身的命名中,并未出现过,可能是和内置了两个加载器有关?非常仔细的朋友可能会发现:说好的类加载,为何会出现关键字resources?我相信在某个概念层次,JDK把Class和Resource看成了统一概念,Class是重点,其他资源则通过资源加载进行封装,如图片、文本、音频等文件资源。

 发散一下:为何需要使用委托模式进行类加载?

5、并行加载:parallel capable


        Class loaders that support concurrent loading of classes are known as parallel capable class loaders and are required to register themselves at their class initialization time by invoking the ClassLoader.registerAsParallelCapable method. Note that the ClassLoader class is registered as parallel capable by default. However, its subclasses still need to register themselves if they are parallel capable. 

In environments in which the delegation model is not strictly hierarchical, class loaders need to be parallel capable, otherwise class loading can lead to deadlocks because the loader lock is held for the duration of the class loading process (see loadClass methods).

          类加载器在类初始化时,通过调用  ClassLoader.registerAsParallelCapable 来标记该加载器支持并行类加载机制。支持该机制的加载器称之为 可并行 的类加载器。需要注意的是,ClassLoader类是默认可并行加载的,但它的子类仍须通过注册接口调用来支持可并行机制,也就是说,可并行机制不可继承。

        在委托结构设计不是很有层次性(如出现闭环委托)的情况下,这些类加载器需要实现并行机制,否则会出现死锁问题。具体可以参考loadClass的函数源码。其实本文的第一张图片就是该函数代码,出现死锁的情况是在 synchronized 代码块上。JDK1.7在将对象级别的锁降级到被加载的类的级别上,也就是说不同的类加载,不需要在等待锁释放,当然,这前提是显示的调用了 ClassLoader.registerAsParallelCapable。查看getClassLoadingLock你会发现,如果没注册可并行机制,返回的对象锁还是当前对象 this。其实对于并行加载,我在工作中很少接触,这里我不打算做太多的发散,但能肯定的是,用的到这个机制的,绝对是对加载性能要求特别高,甚至是一些模块化加载的项目,如 OSGI 这类。这里我只给自己Mark,等真的有实际场景再去体会,我想我会更加深刻。

        到此为止,ClassLoader的主要核心部分代码已经解读得差不多了,其实和其他人说的或者都差不多,但我想说的是,这是官方声明的,不存在误导,还温习了一波英文。关于类加载器,其实有很多思考,比如说为何JVM要使用委托模式加载类?我希望读者和我一起思考,如果你也和我一样有很多疑问,请给我留言,谢谢你的阅读。

相关文章

  • 理解ClassLoader.java

    本节重点针对Java的ClassLoader的部分源码以及注释进行解读。我相信很多时候,我们没必要去速度获取别人给...

  • 理解!理解!!理解!!!

    1、如何理解MVC设计模式 MVC是一种架构模式,M表示MOdel,V表示视图View,C表示控制器Control...

  • 理解“理解”

    教育不是灌输而是揭示 教育就是向智者揭示智慧,对愚者掩盖无知。——安布罗斯 比尔斯 我们的课堂教学要做的是设计表现...

  • 理解理解,再理解

    【今日悦读】 1214-潇潇 书名:财富自由之路 作者:李笑来 篇目:10~12节 收获: 1,人生三大坑 莫名其...

  • 理解不被理解

    一个人的坐在电脑前发呆,不知道搞什么,闭上眼睛,感觉自己很孤独。这种是内心的那种,很不是滋味,不知道什么时候自己才...

  • 理解你的理解理顺理解

    理解你的理解理顺你的理解 生活中因为缺乏准确具体到位的沟通和具体的实施方向和可落地的方式,因为信息传达...

  • 理解不理解

    对于更多人而言,或许他们更看重的是对与错,是与非。这种执念而且经常让一些人变了味,甚至格格不入。 而对于个人而言,...

  • 理解不曾理解的

    上学时从来没把容貌当成大事儿,只是希望脸上的痘痘能少点,至于什么皮肤白、毛孔粗大、黑眼圈、肤色暗淡、脸型、眉形、眼...

  • 理解与被理解

    凡事都设身处地为别人着想,自己不愿做的事情也不强加给别人,理解的核心在于不自我。

  • 理解什么是理解

    《理解什么是理解》-花花 每个人都渴望被理解,希望被理解,不被理解是一件很痛苦的事情。 小朋友不理解妈妈为对自己反...

网友评论

      本文标题:理解ClassLoader.java

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