Parental delegation Model

Source: Internet
Author: User

The class loader describes:From a virtual machine perspective, there are only two kinds of loaders:
    • One is to start the class loader, which is written in C + + and is part of the virtual machine itself;
    • The other is the loader for all other classes, which are written in Java, independent of the virtual machine, and all inherit from the abstract class Java.lang.ClassLoader;
From the Java Developer's point of view, the ClassLoader can be subdivided into three types of system-provided ClassLoader:
  • Start the ClassLoader bootstrap ClassLoader: This type of loader is responsible for adding java_home\lib directories or-xbootclasspath paths specified, and is the class library that the virtual machine recognizes to be loaded into the virtual machine memory.
  • Extension classloader extension ClassLoader: This loader is responsible for loading the class library in the Java_home\lib\ext directory or under the path specified by the Java.ext.dirs system variable, and developers can use the extension classloader directly.
  • Application ClassLoader Application ClassLoader: Commonly referred to as the system ClassLoader, which is responsible for loading the class library specified on the user's classpath, developers can use the ClassLoader directly, if the application does not have its own class loader custom, In general, the Application class loader is used;
Parental delegation ModelThe parent-delegate model of the ClassLoader is JDK1.2 introduced. The parental delegation model is not a mandatory constraint model, but a class loader implementation that Java designers recommend to developers.                                                            If the class loader shown is such a hierarchical relationship, it is called the class loader's parental delegation model. The parental delegation model requires that the class loader must have its own parent ClassLoader in addition to the top-level startup ClassLoader. (In this case, the subclass and the parent class are not generally implemented by inheritance, but rather by combining the code of the parent loader)   The parent delegation Model works: If a class loader receives a class-loaded request, it does not first attempt to load the class itself, Instead, the request is delegated to the loader to complete, and every layer of the classloader is so, so all the load requests should eventually be routed to the top-level startup class loader, and the sub-loader will attempt to load itself only when the parent loader is unable to complete the load request. Parental delegation Model Advantages: The Java class has a hierarchical relationship with precedence as its classloader For example, a class Java.lang.Object it exists in Rt.jar, no matter which ClassLoader is loading the class and eventually is delegated to the startup ClassLoader at the top of the model to load, so the object class is the same class in the various ClassLoader environments of the program. Conversely, if you do not use the parent delegation model and the individual ClassLoader load it yourself, if the user writes a Java.lang.Object class and puts it in classpath, there will be a number of different object classes in the system. The specific implementation of the parental delegation pattern is in the LoadClass () method under the Java.lang.Classloader class:
protectedClass<?> loadclass (String name,Booleanresolve)throwsClassNotFoundException {synchronized(Getclassloadinglock (name)) {//First, check if the class has been loaded. class<?> C =Findloadedclass (name); if(c = =NULL) {                LongT0 =System.nanotime (); Try {                    if(Parent! =NULL) {//if the parent loader exists, call the parent loaderc = Parent.loadclass (name,false); } Else {//mobilize the Startup class loaderc =findbootstrapclassornull (name); }                } Catch(ClassNotFoundException e) {//ClassNotFoundException thrown if class not found//From the Non-null parent class loader                }                if(c = =NULL) {                    //if the parent loader or the Startup class loader cannot load, then call its own Findclass method to load the class                    LongT1 =System.nanotime (); C=Findclass (name); //This is the defining class loader; record the statsSun.misc.PerfCounter.getParentDelegationTime (). Addtime (T1-t0);                    Sun.misc.PerfCounter.getFindClassTime (). Addelapsedtimefrom (t1);                Sun.misc.PerfCounter.getFindClasses (). increment (); }            }            if(Resolve) {resolveclass (c); }            returnC; }

Parental delegation Model

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.