Java thread.getContext().getClassLoader() 唯一性
Java thread.getContext().getClassLoader() uniqueness
线程上下文的类加载器有多独特。是否每次启动线程都会重置?
我们能否始终确保 2 个并行线程永远不会具有相同的上下文类加载器?
我看到像 Axis 这样的框架依赖于此来获取和设置 运行-时间设置变量。
How unique is the thread context's classloader.
连远程都没有。
Is it reset everytime a thread is started?
创建线程时,它没有可以重置的上下文加载器。它将继承父线程的上下文加载器。启动线程不会更改其上下文加载器。
Can we always be sure that 2 parallel threads will never have the same context classloader?
这实际上不太可能。如前所述,线程默认继承父级的加载器,因此除非有人使用不同的加载器显式调用 setContextClassLoader
,否则 ClassLoader.getSystemClassLoader()
返回的默认应用程序 class 加载器将被所有人使用线程。即使在具有不同 class 加载程序的环境中,也不太可能拥有与线程一样多的 class 加载程序。
I see some frameworks like Axis relying on this to get and set run-time setting variables.
这是该功能的主要用途,框架使用当前线程的上下文class加载程序按照惯例,当然,用于加载classes和资源,而不是假设这些加载程序的唯一性。 JVM 从不单独使用此上下文 class 加载程序,因为在 class 中找到的符号引用是通过 定义 class 加载程序 解析的class。这同样适用于 Class.forName(String)
(没有 class 加载程序参数)。它需要代码主动调用 getContextClassLoader()
并使用返回的加载程序进行 class 加载,以使此功能相关。
不要将 class 加载程序与 ThreadLocal
变量混淆。
线程上下文的类加载器有多独特。是否每次启动线程都会重置?
我们能否始终确保 2 个并行线程永远不会具有相同的上下文类加载器?
我看到像 Axis 这样的框架依赖于此来获取和设置 运行-时间设置变量。
How unique is the thread context's classloader.
连远程都没有。
Is it reset everytime a thread is started?
创建线程时,它没有可以重置的上下文加载器。它将继承父线程的上下文加载器。启动线程不会更改其上下文加载器。
Can we always be sure that 2 parallel threads will never have the same context classloader?
这实际上不太可能。如前所述,线程默认继承父级的加载器,因此除非有人使用不同的加载器显式调用 setContextClassLoader
,否则 ClassLoader.getSystemClassLoader()
返回的默认应用程序 class 加载器将被所有人使用线程。即使在具有不同 class 加载程序的环境中,也不太可能拥有与线程一样多的 class 加载程序。
I see some frameworks like Axis relying on this to get and set run-time setting variables.
这是该功能的主要用途,框架使用当前线程的上下文class加载程序按照惯例,当然,用于加载classes和资源,而不是假设这些加载程序的唯一性。 JVM 从不单独使用此上下文 class 加载程序,因为在 class 中找到的符号引用是通过 定义 class 加载程序 解析的class。这同样适用于 Class.forName(String)
(没有 class 加载程序参数)。它需要代码主动调用 getContextClassLoader()
并使用返回的加载程序进行 class 加载,以使此功能相关。
不要将 class 加载程序与 ThreadLocal
变量混淆。