Why is the SetContextClassLoader () method placed in Thread?

Why is the setContextClassLoader() method placed on Thread ?

What different threads do different classloaders have?

The question is, if I expanded ClassLoader , I loaded several new classes there. to my custom classloader.

Now I want it to be a class loader, so I call the Thread.currentThread().setContextClassLoader(loader) method.

Are these new classes available only in the context of the current thread? Or how does it work?

thanks

+4
source share
4 answers

The context class loader is the class loader that the stream will use to search for classes. You primarily care about this when writing an application server or something like that. The idea is that you can start the stream from a class loaded in the application server's class loader, and still pass it a child class loader that handles the class classes of the deployed application.

+6
source

The thread context class loader is a bit hacked.

When you load a class with reflection, you use either an explicit class loader or an immediate call class. When you reference a class using regular Java code, then the class requesting the link is used as the source for the loader.

Thread.setContextClassLoader used to set the class loader for Thread.getContextClassLoader . This is used by random APIs (especially through ServiceLoader ) to collect classes by reflection so that you can change the implementation. Implementing implementations from under your code depending on which thread it launches at a critical moment is a bad idea.

+2
source

Java class loaders can be categorized below

1) Bootstrap class bootloader
Loading classes from JAVA_HOME / jre / lib / rt.jar

2) Class Loader Extenders <
Loading classes from JAVA_HOME / jre / lib / ext

3) system class loader
Application class path

We can create our own class loader and specify our own location from which classes related to ContextClassLoader can be loaded

Hope that gives an idea of ​​why we need to use setContextClassLoader ()

0
source

Thread.setContextClassLoader used to set contextClassLoader, if it is not installed manually, it will be installed in systemClassLoader, which is equal to Launcher.AppClassLoader , this can be checked by checking the source code of Launcher. enter image description here

What is the point of using contextClassLoader?

contextClassLoader provides a back up around the class loading delegation scheme.

The question then becomes, why do we need this back door?

Take JNDI, for example: its guts are implemented by the bootstrap classes in rt.jar (starting with J2SE 1.3), but these core JNDI classes can be loaded by third-party JNDI providers that are potentially deployed in the -classpath application. This script requires the parent class loader (the root cause in this case) to load the class visible to one of its child class loaders (for example, the system one). Delegation with normal J2SE does not work, and a workaround is to make the main JNDI classes using stream context loaders , thereby effectively "tunneling" through the class loader hierarchy in the opposite direction to the correct delegation.

For more information, please check which bootloader class you should use.

0
source

Source: https://habr.com/ru/post/1304774/


All Articles