pre-defined class loaders (three types):
Start (Bootstrap) class loader:
is a class loader implemented with native code that is responsible for loading the class library below <java_runtime_home>/lib into memory (such as Rt.jar).
Because the boot ClassLoader involves the virtual machine local implementation details, the developer cannot directly get a reference to the startup ClassLoader, so it is not allowed to operate directly from the reference.
Extension extension (Extension) class loader:
is implemented by Sun's Extclassloader (Sun.misc.launcher$extclassloader). It is responsible for loading the < Java_runtime_home >/lib/ext or the class library in the specified location java.ext.dir the system variable into memory. Developers can use the standard extension class loader directly.
System class Loader:
is implemented by Sun's Appclassloader (Sun.misc.launcher$appclassloader). It is responsible for loading the class library specified in the System class path (CLASSPATH) into memory. Developers can use the System class loader directly.
Parent delegation Mechanism:
When a particular class loader receives a request to load a class, it first delegates the load task to the parent class loader, recursively, and returns if the parent ClassLoader can complete the class load task, only to load itself when the parent ClassLoader cannot complete the load task. This is a proxy approach.
Parental assignment Meaning:
System class prevents multiple copies of the same byte code in memory
Ensure safe and stable operation of Java programs
Thread Context class loader (special):
The parent delegation model is broken, and the parent-delegate load chain mode can be discarded in the execution thread, allowing the program to reverse-use the ClassLoader.
The methods in class Java.lang.Thread Getcontextclassloader () and Setcontextclassloader (ClassLoader cl) are used to get and set the context class loader for the thread.
If it is not set by the Setcontextclassloader (ClassLoader cl) method, the thread inherits the context class loader of its parent thread. The context class loader for the initial thread that the Java application runs is the system ClassLoader. Code that runs in a thread can load classes and resources through such loaders.
1. When the high-level provides a unified interface for the lower layer to implement, while at the top of the load (or instantiate) the lower class, you must pass the thread context class loader to help the high-level classloader find and load the class.
2. When using this class of managed classes to load, however, loading the classloader of this class is unknown, in order to isolate the different callers, you can take the caller's respective thread context ClassLoader for hosting.
A few questions:
Launch (BOOTSTRAP) class loader It is used to load the Java core library, is implemented with native code, does not inherit from the Java.lang.ClassLoader.
How does a Java virtual machine determine that two Java classes are the same?
The Java virtual machine depends not only on whether the full name of the class is the same, but also on whether the class loader loading this class is the same. The two classes are the same only if they are the same. Even the same byte code is different from the classes that are obtained after the loading of different classloader.
eg
Public voidtestclassidentity () {String Classdatarootpath= "C:\\workspace\\classloader\\classdata"; Filesystemclassloader FSCL1=NewFilesystemclassloader (Classdatarootpath); Filesystemclassloader Fscl2=NewFilesystemclassloader (Classdatarootpath); String ClassName= "Com.example.Sample"; Try{Class<?> Class1 =Fscl1.loadclass (className); Object obj1=class1.newinstance (); Class<?> Class2 =Fscl2.loadclass (className); Object Obj2=class2.newinstance (); Method Setsamplemethod= Class1.getmethod ("Setsample", Java.lang.Object.class); Setsamplemethod.invoke (Obj1, OBJ2); } Catch(Exception e) {e.printstacktrace (); } }
The code uses two different instances of class Filesystemclassloader to load Class Com.example.Sample, obtains two different java.lang.Class instances, and then passes Newinstance () The method generates two classes of objects Obj1 and OBJ2, and finally calls the method Setsample on the object obj1 through the Java reflection API, attempting to assign the object Obj2 to obj1 objects inside instance
Run results
Java.lang.reflect.InvocationTargetException
At Sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
At Sun.reflect.NativeMethodAccessorImpl.invoke (nativemethodaccessorimpl.java:39)
At Sun.reflect.DelegatingMethodAccessorImpl.invoke (delegatingmethodaccessorimpl.java:25)
At Java.lang.reflect.Method.invoke (method.java:597)
At ClassLoader. Classidentity.testclassidentity (classidentity.java:26)
At ClassLoader. Classidentity.main (Classidentity.java:9)
caused by:java.lang.ClassCastException:com.example.Sample
Cannot is cast to Com.example.Sample
At Com.example.Sample.setSample (sample.java:7)
... 6 more
The running result can be seen, the runtime throws a Java.lang.ClassCastException exception. Although the names of the two objects obj1 and obj2 are the same, the two classes are loaded by different class loader instances and are not considered to be identical by the Java virtual machine.
The life cycle of a Java class:
Class starts from being loaded into the virtual machine's memory, and its entire lifecycle includes: Load (Loading), validate (verification), prepare (preparation), Parse (Resolution), Initialize ( initialization), use (using), and unload (unloading) 7 stages. wherein, the preparation, validation, parsing 3 parts collectively referred to as the connection (linking).
Load (Loading):
is to load the class file of the source file into the method area by finding the information for the classes.
A Java.lang.Class object is then instantiated in the heap area as the entry for the information in this class in the method area.
Connection (Linking):
Validation: Determines whether the class conforms to the Java language Specification, has no duplication of attributes and behavior, and whether inheritance is reasonable, in short, to ensure that the JVM can execute
Preparation: The main thing to do is to allocate memory for member variables modified by static and set the default initial value
(1. Eight basic data types the default initial value is 0
2. Default initial value of reference type is NULL
3. A static final modifier is assigned directly, for example: static final int x=10; the default is 10.)
parsing: The task at this stage is to convert a symbolic reference in a constant pool to a direct reference, and it is plain that the JVM converts all class or interface names, field names, method names to specific memory addresses.
Initialize (initialization)
This stage is the process of assigning static variables (class variables), that is, only static modifiers can be initialized, and the order of execution is:
The parent class static domain or static code block, then the subclass static domain or subclass static code block
Use (using)
There are still three steps in the use of a class: Object instantiation, Garbage collection, object finalization
Uninstall (unloading)
The life cycle of the class goes to the last step, the program no longer has a reference to the class, and the class is garbage collected by the JVM, and the life ends.
Reference article:
https://www.ibm.com/developerworks/cn/java/j-lo-classloader/
http://blog.csdn.net/yangcheng33/article/details/52631940
JVM class loader and Java class life cycle