为什么将setContextClassLoader()方法放在线程上? [英] Why is the setContextClassLoader() method placed on Thread?

查看:92
本文介绍了为什么将setContextClassLoader()方法放在线程上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么 setContextClassLoader()方法放在 Thread 上?

哪个不同的线程具有不同的类加载器?

What different thread have different classloaders?

问题是,如果我扩展 ClassLoader ,该怎么办?在那里加载了一些新类。到我的自定义类加载器。

The question is what if I extended a ClassLoader, loaded there some new classes. to the my custom classloader.

现在,我希望它成为上下文类加载器,因此我将方法称为 Thread.currentThread()。setContextClassLoader(loader)

Now , I want it to be the context classloader , so I call the method Thread.currentThread().setContextClassLoader(loader).

这些新类仅在当前线程的上下文中可用吗?或者它是如何工作的?

Are these new classes awailable only in the context of the current thread ? Or how does it work?

谢谢

推荐答案

Context类loader是线程将用于查找类的类加载器。当您编写应用程序服务器或类似的东西时,您主要关心这一点。这样的想法是,您可以从应用程序服务器的类加载器中加载的类启动线程,然后将处理该加载的应用程序类的子类加载器传递给该子类加载器。

The Context class loader is the class loader that the thread will use to find classes. You primarily care about this when you are writing an application server or something similar. The idea is that you can start a thread from a class loaded in the application server's class loader, and yet pass it a child class loader that handles loading the classes of the deployed application.

这篇关于为什么将setContextClassLoader()方法放在线程上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆