Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the setContextClassLoader() method placed on Thread?

Why is the setContextClassLoader() method placed on Thread?

What different thread have different classloaders?

The question is what if I extended a ClassLoader, loaded there some new classes. to the my custom classloader.

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

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

Thanks

like image 755
Roman Avatar asked Mar 22 '10 14:03

Roman


People also ask

What is setContextClassLoader?

setContextClassLoader is used to set the class loader for Thread. getContextClassLoader . This is used by random APIs (notably through ServiceLoader ) to pick up classes through reflection so that you can change implementation.

What is the context ClassLoader in Java?

That is, the context class loader can load the classes that the application can load. This loader is used by the Java runtime such as the RMI (Java Remote Method Invocation) to load classes and resources on behalf of the user application.

What is the purpose of class loader?

Class loaders are responsible for loading Java classes dynamically to the JVM (Java Virtual Machine) during runtime. They're also part of the JRE (Java Runtime Environment). Therefore, the JVM doesn't need to know about the underlying files or file systems in order to run Java programs thanks to class loaders.

How many Classloaders are there in Java?

There are three types of built-in ClassLoader in Java. Bootstrap Class Loader – It loads JDK internal classes. It loads rt. jar and other core classes for example java.


3 Answers

The Context class loader is the class loader that the thread will use to find classes. You primarily care about this when you are writing an application server or something similar. The idea is that you can start a thread from a class loaded in the application server's class loader, and yet pass it a child class loader that handles loading the classes of the deployed application.

like image 175
Yishai Avatar answered Nov 15 '22 15:11

Yishai


The thread context class loader is a little bit of a hack.

When you load a class with reflection, you use either an explicit class loader or the one of the immediate calling class. When you link to a class using normal Java code, then class requesting the linking is used as the source for the loader.

Thread.setContextClassLoader is used to set the class loader for Thread.getContextClassLoader. This is used by random APIs (notably through ServiceLoader) to pick up classes through reflection so that you can change implementation. Having implementations change out from under your code depending upon which thread it is running on at a crucial moment is a bad idea.

like image 37
Tom Hawtin - tackline Avatar answered Nov 15 '22 15:11

Tom Hawtin - tackline


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

Then what is the use of contextClassLoader?

contextClassLoader provides a back door around the classloading delegation scheme.

Then this question becomes why do we need this back door?

From the JavaWorld article Find a way out of the ClassLoader maze

Take JNDI for instance: its guts are implemented by bootstrap classes in rt.jar (starting with J2SE 1.3), but these core JNDI classes may load JNDI providers implemented by independent vendors and potentially deployed in the application's -classpath. This scenario calls for a parent classloader (the primordial one in this case) to load a class visible to one of its child classloaders (the system one, for example). Normal J2SE delegation does not work, and the workaround is to make the core JNDI classes use thread context loaders, thus effectively "tunneling" through the classloader hierarchy in the direction opposite to the proper delegation.

like image 26
Frank Zhang Avatar answered Nov 15 '22 15:11

Frank Zhang