关于“Inside the Java Virtual Machine - Chapter 8 - The Linking Model - Class Loaders and the Parent-Delegation Model”的疑问

2020-09-08 10:02:18 +08:00
 JasonLaw

Inside the Java Virtual Machine - Chapter 8 - The Linking Model - Class Loaders and the Parent-Delegation Model中,有如下这么一段:

In Java terminology, a class loader that is asked to load a type, but returns a type loaded by some other class loader, is called an initiating class loader of that type. The class loader that actually defines the type is called the defining class loader for the type. In the previous example, therefore, the defining class loader for java.io.FileReader is the bootstrap class loader. Class Cindy is an initiating class loader, but so are Mom, Grandma, and even the bootstrap class loader. Any class loader that is asked to load a type and is able to return a reference to the Class instance representing the type is an initiating loader of that type.

"a class loader that is asked to load a type, but returns a type loaded by some other class loader, is called an initiating class loader of that type"和"Any class loader that is asked to load a type and is able to return a reference to the Class instance representing the type is an initiating loader of that type"是不是有点矛盾?

假设 parent-delegation chain 是"L -> Lp -> bootstrap",应用请求 class loader L 加载 java.io.FileReader,L 会 delegate 给 Lp,Lp 会 delegate 给 bootstrap,bootstrap 会成功加载并返回 Class instance 给 Lp,然后 Lp 返回 Class instance 给 L 。

按照上面的例子,我理解的话,“a class loader that is asked to load a type, but returns a type loaded by some other class loader, is called an initiating class loader of that type”意味着“L 和 Lp 是 initiating class loader,而 bootstrap 不是,因为 type 就是被 bootstrap 加载的,不满足 returns a type loaded by some other class loader”。而“Any class loader that is asked to load a type and is able to return a reference to the Class instance representing the type is an initiating loader of that type”则意味着“L 、Lp 和 bootstrap 都是 initiating class loader”。这是书本的错误吗?

Chapter 5. Loading, Linking, and Initializing - 5.3. Creation and Loading也说了“When one class loader delegates to another class loader, the loader that initiates the loading is not necessarily the same loader that completes the loading and defines the class. If L creates C, either by defining it directly or by delegation, we say that L initiates loading of C or, equivalently, that L is an initiating loader of C.”。

956 次点击
所在节点    Java
0 条回复

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/705077

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX