问题描述
为什么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.
如所述:
- 缓存
- 父母
- 自我
这种机制可确保类倾向于由最接近根的类加载器加载。
这篇关于为什么要按升序检查ClassLoader的缓存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!