Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the Java class loading parent delegation mechanism

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the knowledge of "what is the parent delegation mechanism of Java class loading". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Predefined class loader and parent delegation mechanism

Three types of class loaders predefined by JVM:

In addition to the three types of loaders listed above, there is also a special type-thread context class loader.

Bootstrap class loader: a class loader implemented in native code that is responsible for loading class libraries under / lib into memory (such as rt.jar). Because the bootstrap classloader involves the local implementation details of the virtual machine, the developer cannot directly get the reference of the boot classloader, so it is not allowed to operate directly through the reference.

Standard extension (Extension) class loader: implemented by Sun's ExtClassLoader (sun.misc.Launcher$ExtClassLoader). It is responsible for transferring

< Java_Runtime_Home >

/ lib/ext or a class library in the location specified by the system variable java.ext.dir is loaded into memory. Developers can use the standard extension class loader directly.

System (System) class loader: implemented by Sun's AppClassLoader (sun.misc.Launcher$AppClassLoader). It is responsible for loading the class libraries specified in the system classpath (CLASSPATH) into memory. Developers can use the system class loader directly.

Description of parent appointment mechanism

When a specific class loader receives a request to load a class, it first delegates the loading task to the parent class loader, recursively, and returns successfully if the parent class loader can complete the class loading task; only when the parent class loader cannot complete the loading task, it loads it itself.

Some thoughts

The first class loader of the Java virtual machine is Bootstrap, this loader is very special, it is not a Java class, so it does not need to be loaded by others, it is nested in the Java virtual machine kernel, that is, when JVM starts, Bootstrap has already started, it is binary code written in C++ (not bytecode), it can load other classes.

This is why we found that the result of System.class.getClassLoader () is null when testing. This does not mean that System does not have a classloader, but that its loader is special, which is BootstrapClassLoader. Because it is not a Java class, getting its reference must return null.

The specific meaning of entrustment mechanism

When the Java virtual machine is about to load a class, which class loader is sent to load it?

First, the class loader of the current thread loads the first class in the thread (assuming class A).

Note: the classloader for the current thread can be obtained through the getContextClassLoader () of the Thread class, or you can set the classloader yourself through setContextClassLoader ().

If there is a reference to class A, the class Bjm Java Virtual Machine will use the class loader that loads class A to load class B.

You can also directly call the ClassLoader.loadClass () method to specify a class loader to load a class.

The meaning of delegation mechanism-to prevent multiple copies of the same bytecode in memory

For example, both class An and class B load the System class:

If you load your own instead of delegating, class A will load one copy of System bytecode, and class B will load another copy of System bytecode, so that two copies of System bytecode will appear in memory.

If you use the delegate mechanism, it will recursively look up the parent class, that is, the first choice is Bootstrap to try to load, and then go down if it can't be found. The System here can be found and loaded in Bootstrap. If class B also loads System, it also starts with Bootstrap, and Bootstrap finds that System has already been loaded, then you can directly return to the System in memory without reloading, so there is only one copy of System bytecode in memory.

A series of examination questions

Can you write your own class called java.lang.System?

Answer: usually not, but an alternative approach can be taken to meet this requirement.

Explanation: in order to prevent us from writing System classes, class loading uses a delegate mechanism, which ensures that dads have priority, and sons don't have a chance to load the classes that dads can find. The System class is loaded by the Bootstrap loader, and even if it is rewritten by itself, it always uses the System provided by the Java system, and the System class written by itself has no chance to be loaded at all.

However, we can do this by defining a class loader ourselves, which must also be special in order to avoid the parental delegation mechanism. Because the system's three class loaders all load classes in a specific directory, if our own class loader is placed in a special directory, then the system's loader cannot be loaded, that is, it will eventually be loaded by our own loader.

This is the end of the content of "what is the parent delegation mechanism for Java class loading". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report