I'm wondering why Java introduced contexClassLoader for threads. I know that it is usualy used by frameworks and application servers which have to dynamically find and load classes.
However I don't understand why Java introduced contexClassLoader when we can achieve the same functionality simply by adding field that will hold necessary class loader.
class ThreadWithCustomClassloader extends Thread
{
ClassLoader threadClassLoader;
public void run()
{
//use threadClassLoader to dynamically find and load classes
}
}
I'm confused - having an instance variable for the class loader is exactly what the Thread class uses to implement this. What's different in your solution?
Do you mind about the setter? Setting the class loader is important to be able to reuse the same thread (e.g. in the servlet container) for a completely different environment (web application). Thread instances are considered expensive...