Class loading mechanism, parent delegation model

  Class loading mechanism: the virtual machine described class data from the class file is loaded into memory, and verify the data, and converts parsing initialization, forming Java type can be used as a virtual machine.


  Class loader Timing: life cycle class is loaded from the class to the virtual machine memory, up to unload memory.

  Lifecycle class: load loading ---> connect (verification verification prepare preparation parsing resolution) ---> initialization initialization ---> use using ---> Uninstall unloading

  Class load
      loading
          1. Obtain a binary stream of bytes from the file Class
          dynamic structure 2. When the byte stream structure into a static method of operating zone
          3. java.lang.Class objects in memory is generated representative of the Class as a method of such access entry region.
      Connection
          Verification: Verify information byte stream Class file contains JVM meets the requirements, and to ensure that no harm JVM own security.
          Preparation: allocate memory for static variables and assign initial value
          analysis: The constant pool of symbolic reference is converted to a direct reference
      initialization

          clinit call class () method, given the actual values ​​for static variables, static code execution block

  Parents delegated model of the process: a particular class loader to load a class, upon request, the first task entrusted to load the parent class loader, in turn recursion, if the parent class loader class loader can complete the task successfully return; only when the parent class loader can not complete this task load, load it themselves.

  Parents benefit delegation model: it is a Java class as a class loader has together with a priority hierarchy. For example various class java.lang.Object, it exists in rt.jar, whichever class loader to load the class are ultimately assigned to the top in the model Bootstrap ClassLoader loading, so the Object class of the program class loader environments are the same class. Conversely, if there is no parent delegation model but by each class loader to load their own words, if you write the same name as a class java.lang.Object ClassPath and placed in a number of different systems that the Object class will emerge, program will be chaos. Therefore, if developers try to write a Java class with the same name rt.jar class library,
it can be compiled normally, but can never be loaded to run.

 

Only virtual machines in the same class loader and loaded two classes are the same as the class name was determined that this is a case of class. Without applying the parents delegate mechanism, it is possible to load a plurality of the same class is a class loader, which will be identified as such two different classes, there is a problem assigned to one another.

When parents delegate mechanism to ensure multi-loader loads a class, the final is loaded by a loader, ensure that the final load the same result.

Guess you like

Origin www.cnblogs.com/hetaoyuan/p/11401799.html