Thinking about the Java class loading parental delegation mechanism (with an interview question)

Source: Internet
Author: User

Pre-defined ClassLoader and parental delegation mechanisms

&NBSP;JVM predefined three types of ClassLoader:  startup (Bootstrap) class loader: is a class loader implemented with native code that is responsible for <java_runtime_home>/ The class library below Lib is loaded into memory (such as Rt.jar). Because the boot ClassLoader involves the virtual machine local implementation details, the developer cannot directly get a reference to the startup ClassLoader, so it is not allowed to operate directly from the reference. Standard extension (Extension) ClassLoader: is implemented by Sun's Extclassloader (Sun.misc.launcher$extclassloader). It is responsible for loading < java_runtime_home >/lib/ext or the class library in the specified location java.ext.dir the system variable into memory. Developers can use the standard extension class loader directly. System class Loader: is implemented by Sun's Appclassloader (Sun.misc.launcher$appclassloader). It is responsible for loading the class library specified in the System class path (CLASSPATH) into memory. Developers can use the System class loader directly. In addition to the three types of loaders listed above, there is a special type-thread context class loader.   Parent delegation mechanism description   when a particular class loader receives a request to load a class, it first delegates the load task to the parent class loader, recursively, and returns if the parent ClassLoader can complete the class load task, only to load itself when the parent ClassLoader cannot complete the load task.    a few thoughts on  java the first classloader of the virtual machine is bootstrap, which is special, it is not a Java class, so it does not need to be loaded by others, it is nested inside the Java Virtual machine kernel, That is, when the JVM starts up, Bootstrap is started, it is a binary code written in C + + (not a bytecode), it can load other classes.   This is why we found the System.class.getClassLoader () NULL result when testing, which does not mean that the system does not have a class loader, but that it has a special loader. Is Bootstrapclassloader, because it is not a Java class, so getting its reference will definitely return NULL.   delegate mechanism specific meaning   when the Java Virtual machine load a class, what kind of loader is sent to load it?   The class loader of the current thread is first loaded to load the first class in the thread (assumingClass A).   NOTE: The class loader for the current thread can be obtained through the Getcontextclassloader () of the thread class, or you can set the class loader yourself by Setcontextclassloader (). If Class A refers to a class B,java the virtual machine will use the class loader that loads class A to load Class B. You can also call the Classloader.loadclass () method directly to specify a class loader to load a class. The meaning of the delegation mechanism-to prevent multiple copies of the same bytecode in memory   For example, two classes A and Class B load the system class:  if you do not use a delegate but load your own, class A loads a copy of the system bytecode, Class B then loads a copy of the system bytecode so that two copies of the system bytecode are present in memory. If you use a delegate mechanism, you will recursively find the parent class, which is preferred to try to load with bootstrap if it is not found again. The system here can be found in bootstrap and then loaded, and if Class B also loads the system at this point, starting with Bootstrap, At this point, Bootstrap found that the system has been loaded and returned directly to the in-memory system without reloading, so that only one copy of the system's bytecode is in memory.   A face question   can you write a class called Java.lang.System?   Answer: Usually not, but you can take an alternative approach to meet this requirement.   Explanation: In order to not let us write the system class, class loading uses a delegation mechanism, which will ensure that fathers first, fathers can find the class, the son will not have the opportunity to load. The system class is loaded by the bootstrap loader, and even if you rewrite it, you always use the system provided by the Java systems, and the system class you write does not have a chance to get loaded at all.   However, we can define a class loader ourselves to do this, and in order to avoid the parental delegation mechanism, this classloader must also be special. Because the system comes with three ClassLoader that load classes in a particular directory, if our own classloader is placed in a special directory, then the system loader will not load, that is, ultimately, our own loader load.

Thinking about the Java class loading parent delegation mechanism (with an interview question)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.