Thread
类的getContextClassLoader()
方法返回线程的上下文ClassLoader
。
语法
public ClassLoader getContextClassLoader()
返回
- 它返回线程的上下文
ClassLoader
。
异常
SecurityException
:如果当前线程无法获取上下文ClassLoader
。
示例
public class JavaGetClassLoaderExp implements Runnable
{
public void run()
{
System.out.println("Thread is running");
}
public static void main(String args[])
{
JavaGetClassLoaderExp g1 = new JavaGetClassLoaderExp();
Thread t1 = new Thread(g1);
// call run() method
t1.start();
// returns the context ClassLoader for thread t1
ClassLoader loader = t1.getContextClassLoader();
// sets the context ClassLoader for thread t1
t1.setContextClassLoader(loader);
System.out.println("Context ClassLoader = " + loader);
System.out.println("Parent = " + loader.getParent());
System.out.println("Class = " + loader.getClass());
}
}
执行上面示例代码,得到以下结果:
Context ClassLoader = jdk.internal.loader.ClassLoaders$AppClassLoader@7c53a9eb
Parent = jdk.internal.loader.ClassLoaders$PlatformClassLoader@ed17bee
Class = class jdk.internal.loader.ClassLoaders$AppClassLoader
Thread is running