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

Source: Internet
Author: User

pre-defined classloader and parental delegation mechanisms
  1. Three types of class loaders that are pre-defined by the JVM:

      • Startup (Bootstrap) class loader : is a class loader implemented with native code that is responsible for <Java_Runtime_Home>/lib loading the following class library into memory (for example 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) class loader : Is implemented by Sun ExtClassLoader(sun.misc.Launcher$ExtClassLoader) . It is responsible for < Java_Runtime_Home >/lib/ext java.ext.dir loading the class library into memory, either in the specified location of the system variable. Developers can use the standard extension class loader directly.
      • System Class Loader : Is implemented by Sun 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.

  2. 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.

 Some Thoughts
  1. The first class loader of the Java Virtual machine is bootstrap, the loader is 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 the JVM started Bootstrap started, it is in C + + Write the binary code (not the bytecode), it can go to load other classes.

    This is why we found the System.class.getClassLoader() result to be null in the test, which does not mean that the system class does not have a classloader, but that its loader is special, BootstrapClassLoader because it is not a Java class, so getting its references will definitely return NULL.


  2. when Java virtual machines load a class, what kind of loader does it send to load?

    • first class loader of the current thread to load the first class in a thread (assumed Class A) 。
      note: the class loader for the current thread can pass the thread class Getcontextclassloader (), or you can set the class loader yourself by Setcontextclassloader ().
    • can also be called directly Classloader.loadclass () method to specify a class loader to load a class.
  3. the meaning of the delegation mechanism-to prevent multiple copies of the same bytecode from appearing in memory
    For example, two classes A and b all load the system class:

    • If you load yourself without a delegate, class A loads a copy of the system bytecode, and then class B 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, Bootstrap found that the system has been loaded and returned directly to the in-memory system without reloading , so there is only one copy of the system's bytecode in memory.
 A question of a face
    • Can you write a class call java.lang.System ?

      Answer: usually not, but can take the alternative method to achieve this demand.
      Explanation: in order to not let us write the system class, the class load using the delegation mechanism, so that the father will be the first, the 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, the classloader must 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 parental delegation mechanism (with an interview question)

Related Article

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.