java.lang.Thread.getContextClassLoader()%uA0方法返回该线程的上下文类加载器。上下文ClassLoader是在这个线程加载类和资源在运行时使用的代码的线程的创建者提供的。
声明
以下是java.lang.Thread.getContextClassLoader()方法的声明
public ClassLoader getContextClassLoader()
参数
-
NA
返回值
此方法返回该线程的上下文类加载器。
异常
-
SecurityException%uA0-- 如果安全管理器存在并且其checkPermission方法不允许获取上下文类加载器。
例子
下面的例子显示java.lang.Thread.getContextClassLoader()方法的使用。
package com.yiibai import java.lang.* public class ThreadDemo implements Runnable { Thread t ThreadDemo() { t = new Thread(this) // this will call run() function t.start() } public void run() { // returns the context ClassLoader for this Thread ClassLoader c = t.getContextClassLoader() // sets the context ClassLoader for this Thread t.setContextClassLoader(c) System.out.println("Class = " + c.getClass()) System.out.println("Parent = " + c.getParent()) } public static void main(String args[]) { new ThreadDemo() } }
让我们来编译和运行上面的程序,这将产生以下结果:
Class = class sun.misc.Launcher$AppClassLoader Parent = sun.misc.Launcher$ExtClassLoader@35a16869