Thread.currentThread() 类加载器和普通类加载器的区别
Difference between Thread.currentThread() classLoader and normal classLoader
你能告诉我 Thread.currentThread().getContextClassLoader()
和 TestServlet.class.getClassLoader()
之间的区别吗?不要将其标记为重复,也请解释并提供示例何时使用这些
Java 文件:
package com.jar.test;
public class TestServlet {
public static void main(String args[]) {
ClassLoader cls = TestServlet.class.getClassLoader().loadClass(
"com.jar.test.TestServlet");
ClassLoader cls = Thread.currentThread().getContextClassLoader()
.loadClass("com.jar.test.TestServlet");
}
}
Thread.currentThread().getContextClassLoader()
Returns the context
ClassLoader
for this Thread
. The context ClassLoader
is provided by
the creator of the thread for use by code running in this thread when
loading classes and resources. If not set, the default is the
ClassLoader
context of the parent Thread. The context ClassLoader
of
the primordial thread is typically set to the class loader used to
load the application.
Class#getClassLoader()
Returns the class loader for the class. Some implementations may use
null
to represent the bootstrap class loader. This method will return
null
in such implementations if this class was loaded by the bootstrap
class loader.
简而言之:
Thread.currentThread().getContextClassLoader()
is the ClassLoader
of the context of the thread that has been set with setContextClassLoader(ClassLoader cl)
。假设您有一个复杂的 java 应用程序,其层次结构为 ClassLoader
(例如应用程序服务器),并且您希望当前线程加载 类 或来自一个特定 ClassLoader
的资源] 在此层次结构中,您只需将线程的上下文 ClassLoader
设置为此特定的 ClassLoader
.
Class#getClassLoader()
只是 ClassLoader
,您的 Class
实例已从中加载。
Thread.currentThread().getContextClassLoader()
这是当前线程 classloader 并且不依赖于 class 调用它
TestServlet.class.getClassLoader()
这是加载 TestServlet class 的 classloader。
please explain as well as provide me example when to use these
假设您拥有 ClassLoader1 拥有的线程 1 和 ClassLoader2 拥有的线程 2。您可以将 TestServlet class 加载到 Thread2(通过 ClassLoader2),然后将其传递给 Thread1。那时,如果 TestServlet 需要加载 ClassLoader1 拥有的 Class,它将需要使用 Thread.currentThread().getContextClassLoader(),因为它是自己的 ClassLoader 是 ClassLoader2 而不是 ClassLoader1。
你能告诉我 Thread.currentThread().getContextClassLoader()
和 TestServlet.class.getClassLoader()
之间的区别吗?不要将其标记为重复,也请解释并提供示例何时使用这些
Java 文件:
package com.jar.test;
public class TestServlet {
public static void main(String args[]) {
ClassLoader cls = TestServlet.class.getClassLoader().loadClass(
"com.jar.test.TestServlet");
ClassLoader cls = Thread.currentThread().getContextClassLoader()
.loadClass("com.jar.test.TestServlet");
}
}
Thread.currentThread().getContextClassLoader()
Returns the context
ClassLoader
for thisThread
. The contextClassLoader
is provided by the creator of the thread for use by code running in this thread when loading classes and resources. If not set, the default is theClassLoader
context of the parent Thread. The contextClassLoader
of the primordial thread is typically set to the class loader used to load the application.
Class#getClassLoader()
Returns the class loader for the class. Some implementations may use
null
to represent the bootstrap class loader. This method will returnnull
in such implementations if this class was loaded by the bootstrap class loader.
简而言之:
Thread.currentThread().getContextClassLoader()
is the ClassLoader
of the context of the thread that has been set with setContextClassLoader(ClassLoader cl)
。假设您有一个复杂的 java 应用程序,其层次结构为 ClassLoader
(例如应用程序服务器),并且您希望当前线程加载 类 或来自一个特定 ClassLoader
的资源] 在此层次结构中,您只需将线程的上下文 ClassLoader
设置为此特定的 ClassLoader
.
Class#getClassLoader()
只是 ClassLoader
,您的 Class
实例已从中加载。
Thread.currentThread().getContextClassLoader()
这是当前线程 classloader 并且不依赖于 class 调用它
TestServlet.class.getClassLoader()
这是加载 TestServlet class 的 classloader。
please explain as well as provide me example when to use these
假设您拥有 ClassLoader1 拥有的线程 1 和 ClassLoader2 拥有的线程 2。您可以将 TestServlet class 加载到 Thread2(通过 ClassLoader2),然后将其传递给 Thread1。那时,如果 TestServlet 需要加载 ClassLoader1 拥有的 Class,它将需要使用 Thread.currentThread().getContextClassLoader(),因为它是自己的 ClassLoader 是 ClassLoader2 而不是 ClassLoader1。