Java interview knowledge (fifty-seven) class loader

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/qq_33945246/article/details/97284700

Class load: Load -> Connection -> initialization. The connection process can be divided into three steps: Verify -> Prepare -> resolution.

One non-loading phase array class (class loading phase acquisition operation binary byte stream) is controllable strongest stage, to complete this step we can also customize the class loader to control the manner of obtaining the byte stream (Overrides a class loader loadClass () method). Array type is not created by the class loader, which created directly by the Java Virtual Machine.

All classes by the class loader, load role is to load the .class file into memory.


Class loader inheritance hierarchy

Here Insert Picture Description

JVM built three important ClassLoader, in addition to other BootstrapClassLoader by the Java class loader and implement all inherited from java.lang.ClassLoader:

AppClassLoader parent class loader for the ExtClassLoader ExtClassLoader parent class loader is null, null ExtClassLoader does not mean that no parent class loader, but Bootstrap ClassLoader.

  • BootstrapClassLoader (boot class loader): top-level class loading, realized by C ++, is responsible for loading% JAVA_HOME% / jar package and class lib directory or all of the parameters specified class path or -Xbootclasspath.
  • ExtensionClassLoader (extension class loader): responsible for loading classes and jar package directory% JRE_HOME% / lib / ext directory, jar or java.ext.dirs package under system variables specified path.
  • AppClassLoader (application class loader): for our users loader is responsible for loading all current packages and classes in the jar application classpath

Parent delegation model

Here Insert Picture Description

  • Concept:
    when a task class loader receives a class loading, load does not expand immediately, but will load the task entrusted to it by the parent class loader to perform each layer type are used in the same manner, until entrusted to boot up the topmost class loader. If the parent class loader can not load entrusted to it by the class ((its search did not find the need to load the class)), load the task to return to class put under a class loader to perform the load.

  • Advantage
    can effectively ensure global uniqueness of a class, when the same class name appears more limited program, the class loader when performing loading, always will load only one of these classes. Java class along with its class loader equipped with a priority hierarchy .
    For example class java.lang.Object, it is stored in rt.jar, whichever class loader to load this class are ultimately entrusted to the model in the top of the boot class loader to load, so the Object class in the program various loader environments are the same class. On the contrary, if no parent delegation model, by each class loader to load their own words, if you have written a ClassPath called java.lang.Object class and placed in the program, that the system will appear more than different Object class, Java type system, the most basic of behavior will not be guaranteed, the application will also become a mess. If you go to write a Java class library rt.jar with the same name already exists in the class, you will find normal compile, but can never be loaded to run.

  • Achieve
    implementation code entrusted parents are concentrated in java.lang.ClassLoader the loadClass () method, logic clear and easy to understand: whether check has been loaded before, if not loaded to call the parent class loader of the loadClass () method, if the parent empty loader used as the parent class loader boot loader default. If the parent class loader fails to load, throw a ClassNotFoundException, then call their findClass method for loading.

Guess you like

Origin blog.csdn.net/qq_33945246/article/details/97284700