Java - Delegation Hierarchy Algorithm

分享一个大牛的人工智能教程。零基础!通俗易懂!风趣幽默!希望你也加入到人工智能的队伍中来!请点击http://www.captainbed.net 

In this article, we will discuss about Types of Class Loaders and How Class Loader uses Parent delegation Algorithm to load the class. If you have not gone through previous article on JVM Internals, I would encourage you to read that first.

In interview room, we often come across the questions related to JVM architecture like,

  1. Which class loading mechanism or algorithm JVM uses to locate the class or
  2. What are the Types of Class Loaders JVM uses to load classes into Method area and so on.

You will find answer of all such Class Loader related questions in this article. Stay tuned.

The mechanism that enabled dynamic loading in Java is a class loader which is one of the cornerstones of Java dynamism. Class loaders are responsible for determining when and how classes can be added to a running Java environment, as well as making sure that important parts of the Java runtime environment are not replaced by impostor code.

You can also develop a custom class loader that is, capable of checking a digital signature before executing untrusted foreign code.

In this article, we will discuss about following

  1.  Types of Class Loader
  2.  How Class Loader works

 

Types Of Class Loader

Class Loader Subsytem contains following three types of class Loaders

  1. Bootstrap Class Loader or primordial Class Loader
  2. Extension Class Loader
  3. Application Class Loader or System Class Loader

Java - Delegation Hierarchy Algorithm_第1张图片

Bootstrap Class Loader

  • It is responsible to load core Java API classes i.e. the classes present in rt.jar, which is present at path (jdk\jre\lib\rt.jar).
  • This location is called bootstrap class path i.e. Bootstrap Class Loader is responsible to load classes from bootstrap class path.
  • Bootstrap Class Loader is by default available with every JVM.
  • It is implemented in native languages like C / C++ and not implemented in java.

 

Extension Class Loader

  • It is the child class of Bootstrap Class Loader.
  • It is responsible to load classes from extension class path (jdk\jre\lib\ext)
  • It is implemented in java and the corresponding .class file is sun.misc.Launcher$ExtClassLoder.class

 

Application Class Loader / System Class Loader

  • It is the child class of Extension Class Loader.
  • This class loader is responsible to load classes from application class path.
  • It internally uses environment variable CLASSPATH.
  • It is implemented in java and the corresponding .class file name is sun.misc.Launcher$AppClassLoader.class

 

How Class Loader Works

 

  • ClassLoader follows delegation hierarchy algorithm. Whenever JVM come across  particular class, first it will check whether the corresponding .class file is loaded or not.
  • If it is already loaded in method area, then JVM will consider that loaded class.
  • If it is not loaded then JVM request class loader sub sytem to load that particular class.
  • Then class loader subsystem handovers the request to Application class loader.
  • Application class loader delegates the request to Extension class loader which in turn delegates the request to Bootstrap class loader.
  • Then Bootstrap class loader will search in Bootstrap class path. If it is available then the corresponding .class will be loaded by Bootstrap class loader.
  • If it is not available then Bootstrap class loader delegates the request to Extension class loader.
  • Extension class loader will search in Extension class path. If is is available, then it will be loaded otherwise extension class loader delegates the request to application class loader.
  • Application class loader will search in Application class path. If it is available, then it will be loaded otherwise we will get runtime exception saying ClassNotFoundException.

Important Point to Note:

  • Classes loaded by a child class loader have visibility into classes loaded by its parent class loaders. So classes loaded by System ClassLoader have visibility into classes loaded by Extensions and Bootstrap ClassLoader.

你可能感兴趣的:(Java)