Influenced by various circumstances, I began to look at the KNOWLEDGE of the JVM again.

1, Java is too much volume, can not go to the deep learning.

2, the interview questions asked many, forced to learn.

3. Pure curiosity. Like a sentence: “eight hours for life, eight hours for development.”

— I hope that when I meet you on farewell Day, you will have achieved something.

'

Cover location: Hunan – small city shaoyang

He is a blogger at 🤸♂️

You deserve it!!

An overview,

The parent delegate mechanism is that when a classloader needs to load a.class bytecode file, it first delegates the task to its parent classloader, recurses the operation, and only loads the.class if the parent has not loaded the.class file. This is a task delegation pattern.

Two, the principle of parent delegation mechanism

Principle:

  • If a classloader receives a request to load a class, the first thing it does is not load, but delegate the request to the parent class’s loader.
  • If the parent class loader also has a parent class loader, then the request will be further delegated up, recursively, and the request will eventually reach the top-level startup class loader;
  • If the parent class loader is able to complete the class load, it returns success. If the parent class loader is unable to complete the class load, the child loader will try to load the class itself. This is the parent delegate mode.

Little story

Here’s a short story to impress:

You see a chicken leg on the table, see the hemp is not very happy. You say: mom, you are the first time this drumstick. Mom saw the grandma on the table again, and said to mom: This drumstick to mend the body. Grandma said happily: my daughter-in-law filial piety ah, I also come to taste the filial taste of the chicken leg today.

You see a chicken leg on the table, see the hemp is not very happy. You say: mom, you are the first time this drumstick. Mom saw grandma on the table again, and said to mom: This chicken leg to mend the body. Grandma said happily: my daughter-in-law has this filial piety heart, you go to work every day to take away, is not good for the body, this drumstick for you. The hemp gets the hand, again backhand the drumstick clip to me, say: this drumstick still have you time, you are growing body now, do not fill a fill long not high. ⛹ ️ came ️

== This is the parent delegation mechanism, I don’t know if you understand after reading this little table story. = =

Here is:

Explanation of code:

I created a java.lang package in my own project, and then I created a String class.

Prepare another test class that references the String class.

The String class is a static block of code.

The code can run with the following output:

It doesn’t print the static block in my String, proving that I’m still using the JDK.

The reason?

Step by step analysis.

We define a custom class. If you want to load it, you should use the Application ClassLoader to load it. But this is where parental delegation comes in.

1. When we want to load this custom String,

First, the Application ClassLoader is loaded, but the Extension ClassLoader is also loaded.

3. Then delegate to the Extension ClassLoader and suddenly discover that it has a Bootstrap ClassLoader.

4. Then delegate to the Bootstrap ClassLoader. Start the loader to see, this is not the java.lang package, this is my task ah, hurry to load it, and then successful return. So the new String() used here is actually using the Java String.

What kind of problems can this prevent?

Java, integer.java, and date.java. If these core classes can be changed at will, the entire project will probably crash. Secondly, the stability of the Java virtual machine will be affected.

The role of parent delegation mechanism

  • Secure the Java core class librariesAll Java applications at least reference the Java.lang. Object class, that is, at runtime, the java.lang.Object class is recorded in the Java Virtual Machine; If this loading process is done by the Java application’s own class loader, then there may be multiple versions of the Java.lang. Object class in the JVM that are incompatible and invisible to each other (because of the namespace). With the help of the parent delegation mechanism, the loading of classes in the Java core class libraries is done uniformly by the startup class loader, thus ensuring that all Java applications are using the same version of the Java core class libraries and are compatible with each other.
  • Ensures that classes provided by the Java core class libraries are not replaced by custom classes.
  • Different class loaders can create additional namespaces for classes with the same binary name. Classes with the same name can exist in the Java Virtual Machine by adding them with different class loaders. Classes loaded by different class loaders are incompatible, creating separate Java class Spaces within the Java virtual Machine.
  • Prevent duplicate loading of the same.class. You can go up there and say, well, I loaded it, so I don’t have to load it again. Ensure data security.

self-talk

Paddling of a ha, we are refueling, unbridled love!!

Let’s go to the end together

'