.NET 进程中可以存在的线程数是否有硬性限制?
可能的重复:
.NET 应用程序中的最大线程数?
我们可以在 .NET 应用程序中创建的线程数量有限制吗?
我假设可以创建的线程数量受到可用内存量的限制,因为需要分配线程堆栈。如果我错了,请纠正我。还有其他因素限制线程数量吗?或者,线程数是否限制为特定数量?
如果我知道机器规格,如何(粗略地)计算机器上可以创建的最大线程数?
Possible Duplicate:
Maximum number of threads in a .NET app?
Is there a limit on the number of threads we can create in a .NET application ?
I am assuming that the number of threads that can be created is limited by the amount of memory available since the threads' stack needs to be allocated. Please correct me if I am wrong. Are there other factors that limits the number of threads ? Or, is the number of threads limited to a specific number ?
How can I (roughly) calculate the maximum number of threads that can be created on a machine, if I know the machine specifications ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一如既往,Raymond Chen 在他的博客。请注意,他的测试似乎是使用非托管代码运行的。我的猜测是,.NET 框架中没有任何内容实际上限制每个进程的线程数,并且该限制将由操作系统强制执行。如果情况确实如此,那么他的测试仍然有效。
另外,我不确定 32 位和 64 位机器之间是否有所不同,我想他的结果取决于 RAM 大小和 32 位/64 位 CPU 以及可能的 CPU 数量。综上所述,看起来他能够创建 13000 个线程。
运行 13k 线程的最大问题是 上下文切换 所花费的时间肯定会耗尽所有时间可用的 CPU 很可能无法完成任何工作。
如果您正在研究的应用程序正在创建大量本应执行大量工作的线程,那么您可能不会遇到进程挂起的情况,而是会遇到上下文切换量问题。显然,多线程应用程序中最常见的问题是资源死锁,但有许多工具可用于解决该情况。
请尝试以下有关死锁的链接,以确定这是否是您实际遇到的情况:
As always, Raymond Chen has the answer on his blog. Note that his test appears to have been run using unmanaged code. My guess is that there's nothing in the .NET framework that actually limits the number of threads per process and that the limit would be enforced by the O/S. If that's truly the case then his test is still valid.
Also, I'm not sure if it's different between 32-bit and 64-bit machines, I would imagine his results are dependent on RAM size and 32bit/64bit CPU along with possibly the number of CPUs. All that said, it looks like he was able to get 13000 threads created.
The big issue with 13k threads running is that the time spent context switching is sure to eat up all the available cpu and you're likely to be getting little to no work done.
If the application you're looking into is creating a lot of threads that are supposed to be doing intense work, you might not be getting a process hang as much as running into issues with the amount of context switching taking place. Obviously the most common issue in a multi-threaded application is a resource deadlock, but there are many tools available to troubleshoot that scenario.
Try the following links about deadlocks to determine if that's what you're actually running into: