Class Loader
Java class loaders are dynamically loading the required classes in the JVM at run time, and the Java class loader is based on three mechanisms: delegate, visible, single.
Load the. class files under the classpath into memory and become bytecode after processing, which is done by the ClassLoader.
- A delegate mechanism refers to passing a request for an add-in class to the parent loader, and then loading it if the parent loader cannot find or load the class.
- The visibility mechanism refers to classes that are loaded by the parent loader that can be seen by the quilt loader, but the class parent loader loaded by the child loader is invisible.
- A single mechanism refers to a class that can only be loaded once by the same loader.
Default class Loader
System default three class loaders:
- BootStrap
- Extclassloader
- Appclassloader
The class loader is also a Java class, and Bootstrap is not. Validation code:
public class Classloadertest {public
static void Main (string[] args) {
System.out.println ( System.class.getClassLoader ());
}
Output: null
If System.out.println (System.class.getClassLoader (). toString) is used, a null pointer exception is reported:
Exception in thread ' main ' java.lang.NullPointerException at
Com.iot.classloader.ClassLoaderTest.main ( CLASSLOADERTEST.JAVA:10) at
sun.reflect.NativeMethodAccessorImpl.invoke0 (Native method)
at Sun.reflect.NativeMethodAccessorImpl.invoke (nativemethodaccessorimpl.java:62) at
Sun.reflect.DelegatingMethodAccessorImpl.invoke (delegatingmethodaccessorimpl.java:43) at
Java.lang.reflect.Method.invoke (method.java:483) at
Com.intellij.rt.execution.application.AppMain.main ( appmain.java:144)
Visible, the system class is loaded by the Bootstrap class loader.
delegate mechanism of class loader
Tree diagram of class loader
Class Loader
The order of the general load classes:
- The class loader of the current thread first loads the first class in the thread
- If Class A applies a class B,java virtual machine will load Class B using the class loader of load Class A
- You can also call the Classloader.loadclass () method directly to develop a class loader to load a class
The principle of writing a custom class loader
Api:
Class ClassLoader
Template Method Design Pattern
Parent class:
LoadClass (class-loaded process, template)
Findclass class load logic that is called by the LoadClass method for child class coverage
DefineClass get class file to convert to byte code
Subclasses: Overriding Findclass methods
Example:
The source code of the LoadClass method
Protected class<?> loadclass (String name, Boolean resolve) throws ClassNotFoundException {synchronized (GETCLA Ssloadinglock (name)) {//I, check if the class has already been loaded class<?> C = findloadedclass (nam
e);
if (c = = null) {Long T0 = System.nanotime ();
try {if (parent!= null) {c = Parent.loadclass (name, false);
else {c = findbootstrapclassornull (name); The catch (ClassNotFoundException e) {//ClassNotFoundException thrown if class not found//from T He non-null parent class loader} if (c = null) {//If still not found, then invoke Findclass in or
Der//To find the class.
Long T1 = System.nanotime ();
c = findclass (name); This is the defining class loader;
Record the stats sun.misc.PerfCounter.getParentDelegationTime (). Addtime (T1-T0); Sun.misc.PerfCounter.getFindClassTime (). AddelapseDtimefrom (t1);
Sun.misc.PerfCounter.getFindClasses (). increment ();
} if (resolve) {resolveclass (c);
return C;
}
}
Examples in the API documentation:
Class Networkclassloader extends ClassLoader {
String host;
int port;
Public Class Findclass (String name) {
byte[] b = loadclassdata (name);
Return DefineClass (name, b, 0, b.length);
Private byte[] loadClassData (String name) {
//Load the class data from the connection ...
}
}