Mutant World

Thursday, July 13, 2006

Why Thread.currentThread().getContextClassLoader() ?

I recently been involved in a discussion where I questioned that Thread.currentThread().getContextClassLoader() was the right way to implement the accessibility of a ClassLoader from any point in the code from a thread.
I very much preferred that ClassLoader.getContextClassLoader() was available, thus freeing class Thread of any reference to class loading.

Other examples of accessibility of thread local information are present in the Java libraries, most notably TransactionManager.getTransaction(), that returns the Transaction currently associated with the thread (though the call is not static).
There is also a RFE for having the Locale available, see RFE 6197800.

Both follow the model of not being implemented in class Thread, so I wonder why for ClassLoader it was implemented in this way. Some historical reason maybe, and probably right now is not practical to deprecate that call in favor of ClassLoader.getContextClassLoader().