当前位置:网站首页>Talk about the parental delegation mechanism in detail (often asked in interviews) [easy to understand]

Talk about the parental delegation mechanism in detail (often asked in interviews) [easy to understand]

2022-07-23 19:35:00 Full stack programmer webmaster

Hello everyone , I meet you again , I'm your friend, Quan Jun .

Preface

Java Virtual machines are right class The document uses Load on demand The way , That is to say, the class will be used only when it is needed class File loading into memory generation class object , and , Load the of a class class When you file ,Java The virtual machine uses Parent delegate mechanism , namely Leave the request to the parent class , It's a task delegation model

working principle

(1) If a class loader receives a class load request , It doesn't load itself first , Instead, the request is delegated to the loader of the parent class

(2) If the parent loader still has its parent loader , Then further entrust , Recursion in turn , The request will eventually reach the top-level boot class loader ;

(3) If the parent loader can complete the class loading task , You're back , If the parent loader cannot complete the loading task , Sub loader will try to load by itself , This is the parent delegation mechanism

(4) The parent loader allocates tasks one layer at a time , If the subclass loader can load , Then load this class , If the load task is assigned to the system class, the loader cannot load this class , Throw an exception

Code example

give an example 1 :

I built one myself java.lang.String class , write static Code block

package java.lang;

public class String { 
   
    static{ 
   
        System.out.println(" I'm custom String Static code block of class ");
    }
}

Load... In another program String class , Look at the loaded String Class is JDK Self contained String class , We wrote it ourselves String class

public class StringTest { 
   

    public static void main(String[] args) { 
   
        java.lang.String str = new java.lang.String();
        System.out.println("hello,atguigu.com");

        StringTest test = new StringTest();
        System.out.println(test.getClass().getClassLoader());
    }
}

The program doesn't output the contents of our static code block , So what's still loaded is JDK Self contained String class

Why? ? Because we define String Class loader of this application system , But it doesn't load itself first , Instead, the request is delegated to the loader of the parent class , To the extension class loader discovery String Class is not under your own control , Then delegate to the parent class loader ( Boot class loader ), It turns out that java.lang package , This belongs to the boot loader , So what's loaded is JDK Self contained String class

give an example 2 :

In our own String Entire in class main() Method

package java.lang;

public class String { 
   
    static{ 
   
        System.out.println(" I'm custom String Static code block of class ");
    }
    // error :  In the class  java.lang.String  I can't find it in China.  main  Method 
    public static void main(String[] args) { 
   
        System.out.println("hello,String");
    }
}

Because the parent delegation mechanism finds JDK Self contained String class , But in the core class library of the boot class loader API Inside String There is no main() Method

give an example 3:

stay java.lang Wrap the whole ShkStart class ( Custom class name )

package java.lang;

public class ShkStart {
    public static void main(String[] args) {
        System.out.println("hello!");
    }
}

Out of the protection mechanism ,java.lang We are not allowed to customize classes under the package

The advantages of parental appointment mechanism

Through the example above , We can know , The parental mechanism can

  • Avoid duplicate loading of classes
  • Protect program security , Prevention core API Be tampered with at will
    • Custom class :java.lang.String ( useless )
    • Custom class :java.lang.ShkStart( Report errors : Block creation java.lang Initial class )

Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/126800.html Link to the original text :https://javaforall.cn

原网站

版权声明
本文为[Full stack programmer webmaster]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/204/202207231756257545.html