C# .NET 应用程序中的最大线程数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/145312/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Maximum number of threads in a .NET app?
提问by
What is the maximum number of threads you can create in a C# application? And what happens when you reach this limit? Is an exception of some kind thrown?
您可以在 C# 应用程序中创建的最大线程数是多少?当你达到这个限制时会发生什么?是否抛出某种异常?
采纳答案by Mitch Wheat
There is no inherent limit. The maximum number of threads is determined by the amount of physical resources available. See this article by Raymond Chenfor specifics.
没有固有限制。最大线程数由可用物理资源量决定。有关详细信息,请参阅Raymond Chen 的这篇文章。
If you need to ask what the maximum number of threads is, you are probably doing something wrong.
如果您需要询问最大线程数是多少,您可能做错了什么。
[Update: Just out of interest: .NET Thread Pool default numbers of threads:
[更新:只是出于兴趣:.NET 线程池默认线程数:
- 1023 in Framework 4.0 (32-bit environment)
- 32767 in Framework 4.0 (64-bit environment)
- 250 per core in Framework 3.5
- 25 per core in Framework 2.0
- Framework 4.0 中的 1023(32 位环境)
- Framework 4.0 中的 32767(64 位环境)
- 框架 3.5 中每个内核 250 个
- Framework 2.0 中每个内核 25 个
(These numbers may vary depending upon the hardware and OS)]
(这些数字可能因硬件和操作系统而异)]
回答by Ash
Jeff Richter in CLR via C#:
Jeff Richter 通过 C# 在 CLR 中:
"With version 2.0 of the CLR, the maximum number of worker threads default to 25 per CPU in the machine and the maximum number of I/O threads defaults to 1000. A limit of 1000 is effectively no limit at all."
“在 CLR 2.0 版中,机器中每个 CPU 的最大工作线程数默认为 25,而 I/O 线程的最大数量默认为 1000。1000 的限制实际上根本没有限制。”
Note this is based on .NET 2.0. This may have changed in .NET 3.5.
请注意,这是基于 .NET 2.0。这可能在 .NET 3.5 中发生了变化。
[Edit] As @Mitch pointed out, this is specific to the CLR ThreadPool. If you're creating threads directly see the @Mitch and others comments.
[编辑] 正如@Mitch 指出的,这是特定于 CLR 线程池的。如果您要直接创建线程,请查看 @Mitch 和其他人的评论。
回答by antonio
Mitch is right. It depends on resources (memory).
米奇是对的。这取决于资源(内存)。
Although Raymond's article is dedicated to Windows threads, not to C# threads, the logic applies the same (C# threads are mapped to Windows threads).
尽管 Raymond 的文章专用于 Windows 线程,而不是 C# 线程,但逻辑适用相同(C# 线程映射到 Windows 线程)。
However, as we are in C#, if we want to be completely precise, we need to distinguish between "started" and "non started" threads. Only started threads actually reserve stack space (as we could expect). Non started threads only allocate the information required by a thread object (you can use reflector if interested in the actual members).
但是,就像我们在 C# 中一样,如果我们想要完全精确,我们需要区分“已启动”和“未启动”线程。只有启动的线程实际上保留了堆栈空间(正如我们所料)。非启动线程只分配线程对象所需的信息(如果对实际成员感兴趣,可以使用反射器)。
You can actually test it for yourself, compare:
您实际上可以自己测试,比较:
static void DummyCall()
{
Thread.Sleep(1000000000);
}
static void Main(string[] args)
{
int count = 0;
var threadList = new List<Thread>();
try
{
while (true)
{
Thread newThread = new Thread(new ThreadStart(DummyCall), 1024);
newThread.Start();
threadList.Add(newThread);
count++;
}
}
catch (Exception ex)
{
}
}
with:
和:
static void DummyCall()
{
Thread.Sleep(1000000000);
}
static void Main(string[] args)
{
int count = 0;
var threadList = new List<Thread>();
try
{
while (true)
{
Thread newThread = new Thread(new ThreadStart(DummyCall), 1024);
threadList.Add(newThread);
count++;
}
}
catch (Exception ex)
{
}
}
Put a breakpoint in the exception (out of memory, of course) in VS to see the value of counter. There is a very significant difference, of course.
在VS中的异常(当然是内存不足)中放置一个断点以查看计数器的值。当然,有一个非常显着的差异。
回答by Ian Boyd
You should be using the thread pool (or async delgates, which in turn use the thread pool) so that the system can decide how many threads should run.
您应该使用线程池(或异步德尔门,后者又使用线程池),以便系统可以决定应该运行多少线程。
回答by MadApples
i did a test on a 64bit system with c# console, the exception is type of out of memory, using 2949 threads.
我在带有 c# 控制台的 64 位系统上进行了测试,例外是内存不足类型,使用 2949 个线程。
I realize we should be using threading pool, which I do, but this answer is in response to the main question ;)
我意识到我们应该使用线程池,我这样做了,但这个答案是对主要问题的回应;)
回答by Yousha Aleayoub
You can testit by using this snipped code:
您可以使用以下代码进行测试:
private static void Main(string[] args)
{
int threadCount = 0;
try
{
for (int i = 0; i < int.MaxValue; i ++)
{
new Thread(() => Thread.Sleep(Timeout.Infinite)).Start();
threadCount ++;
}
}
catch
{
Console.WriteLine(threadCount);
Console.ReadKey(true);
}
}
Beware of 32-bit and 64-bit mode of application.
谨防 32 位和 64 位模式的应用程序。
回答by ivke
I would recommend running ThreadPool.GetMaxThreads method in debug
我建议在调试中运行 ThreadPool.GetMaxThreads 方法
ThreadPool.GetMaxThreads(out int workerThreadsCount, out int ioThreadsCount);
Docs and examples: https://docs.microsoft.com/en-us/dotnet/api/system.threading.threadpool.getmaxthreads?view=netframework-4.8
文档和示例:https: //docs.microsoft.com/en-us/dotnet/api/system.threading.threadpool.getmaxthreads?view=netframework-4.8