AppDomain 是否在自己的线程中执行?
如果我运行此代码,每个 AppDomain
是否会在不同的线程中执行?
ThreadPool.QueueUserWorkItem(delegate
{
/// Create AppDomain and run code
});
If I run this code, will each AppDomain
execute in a different thread?
ThreadPool.QueueUserWorkItem(delegate
{
/// Create AppDomain and run code
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
默认情况下,AppDomain 不会获得自己的线程。您可以使用现有线程在另一个 AppDomain 中执行代码,或者调用 AppDomain 中创建新线程的方法。事实上,除非您专门创建额外的线程,否则调用另一个域中的代码将在进程的主线程上执行。
来自 AppDomain 文档
在您的示例中,您创建线程(或更具体地说,线程池这样做),因此代码将在这些线程上运行。但是,我不确定是否会建议在这样的线程池线程上创建 AppDomain。
卸载 AppDomain 将中止 AppDomain 中的所有线程。老实说,我不知道线程池对此有何反应。有关卸载的更多信息请参见此处。
AppDomains do not get their own thread per default. You may execute code in another AppDomain using existing threads or call a method in the AppDomain, that creates new thread(s). In fact, unless you specifically creates additional threads calling code in another domain will execute on the process' main thread.
From the AppDomain documentation
In your example, you create threads (or more specifically the thread pool does so) and thus the code will run on these threads. However, I am not sure I would recommend creating AppDomains on thread pool threads like that.
Unloading an AppDomain will abort any threads in the AppDomain. I honestly don't know how the thread pool will react to this. More info about unloading here.
应用程序域是比线程大但比进程小的东西。您可以将它们视为多个线程的潜在集合。如果一个应用程序域创建另一个新的应用程序域,则新的应用程序域将拥有自己的线程。一个应用程序域中的线程永远不会成为另一个应用程序域的一部分,也不允许它直接与其他应用程序域中的线程通信。
An App Domain is something larger than a thread, but smaller than a process. You could think of them as potentially collections of several threads. If an App Domain creates another, new App Domain, the new App Domain will have it's own thread. A thread in one App Domain will never also be part of another App domain, nor will it be allowed to talk directly to threads from other App Domains.