为什么要按升序检查ClassLoader的缓存? [英] Why is ClassLoader's cache checked in ascending sequence?

查看:105
本文介绍了为什么要按升序检查ClassLoader的缓存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么ClassLoader的缓存按升序检查,而类加载却按降序降序?

Why is ClassLoader's cache checked in ascending sequence while class-loading befalls in descending sequence?

推荐答案

Java中的ClassLoader遵循以下三个原则:委托,可见性和独特性。委托原则将类加载的请求转发给父类加载器,并且如果父类无法找到或加载类,则仅加载该类。可见性原则允许子类加载器查看父ClassLoader加载的所有类,但父类加载器看不到子类加载器的类。唯一性原则允许只一次加载一个类,这基本上是通过委派实现的,并确保子ClassLoader不会重新加载父级已经加载的类。

ClassLoader in Java works on three principle: delegation, visibility and uniqueness. Delegation principle forward request of class loading to parent class loader and only loads the class, if parent is not able to find or load class. Visibility principle allows child class loader to see all the classes loaded by parent ClassLoader, but parent class loader can not see classes loaded by child. Uniqueness principle allows to load a class exactly once, which is basically achieved by delegation and ensures that child ClassLoader doesn't reload the class already loaded by parent.

此处所述:


Java中的类加载器以树的形式组织。通过请求,类
的加载器确定该类过去是否已加载,
在其自己的缓存中查找。如果该类存在于高速缓存中,则
CL返回该类,如果不存在,则将请求委托给父级。
如果未设置父级(为Null)或无法加载类,并且
抛出ClassNotFoundException,则类加载器将尝试加载
类本身,并在其自身的路径中搜索类文件。如果可以加载
类,则返回它,否则将抛出ClassNotFoundException
从子级到
父级进行递归的高速缓存查找,直到到达树根或在高速缓存中找到一个类为止。

如果已到达根级,则类加载器尝试加载类,然后
展开从父级到子级的递归。总结以下订单中有

The class loaders in Java are organized in a tree. By request a class loader determines if the class has already been loaded in the past, looking up in its own cache. If the class is present in the cache the CL returns the class, if not, it delegates the request to the parent. If the parent is not set (is Null) or can not load the class and throws a ClassNotFoundException the classloader tries to load the class itself and searches its own path for the class file. If the class can be loaded it is returned, otherwise a ClassNotFoundException is thrown. The cache lookup goes on recursively from child to parent, until the tree root is reached or a class is found in cache. If the root is reached the class loaders try to load the class and unfold the recursion from parent to child. Summarizing that we have following order:


  • 缓存


    • 父母

    • 自我

    这种机制可确保类倾向于由最接近根的类加载器加载。

    这篇关于为什么要按升序检查ClassLoader的缓存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆