[Study notes] JVM thread context class loader

There are many places to see the thread context class loader is provided, for example in the constructor sun.misc.Launcher class, the following code can be seen

 

 

An example of the establishment of the first to write perceptual

public class Test {
    public static void main(String[] args) {
        System.out.println(Thread.currentThread().getContextClassLoader());
        System.out.println(Thread.class.getClassLoader());
    }
}

Export

sun.misc.Launcher$AppClassLoader@18b4aac2
null

This is because the Thread class is in rt.jar package, while the current thread context loader, which is loaded with the loader class where the main function, in fact, the system class loader

 

The current concept of class loader (Current ClassLoader) of

Each class will use its own class loader (i.e., the class loader to load itself) depends to load other classes. For example ClassX cited ClassY, then ClassX class loader to load will ClassY, provided that ClassY not yet been loaded.

 

Class loader thread context (Context ClassLoader)

Thread context class loader is introduced from the beginning JDK1.2, the Thread class getContextClassLoader () in setContextClassLoader (ClassLoader cl) were used to get and set context class loader.

If not set through setContextClassLoader (ClassLoader cl), then the thread will inherit the parent thread context class loader's.

The initial thread Java application runtime context loader is the system class loader. Code thread running, you can load classes and resources by the class loader.

 

The importance of thread context class loader

 

 

Guess you like

Origin www.cnblogs.com/heben/p/11455768.html