简体   繁体   English

ThreadPool SetMaxThreads和SetMinThreads Magic Number

[英]ThreadPool SetMaxThreads and SetMinThreads Magic Number

Is there a magic number or formula for setting the values of SetMaxThreads and SetMinThreads for ThreadPool? 是否有一个神奇的数字或公式来设置ThreadPool的SetMaxThreads和SetMinThreads的值? I have thousands of long-running methods that need execution but just can't find the perfect match for setting these values. 我有成千上万个需要执行的长时间运行方法,但却无法找到设置这些值的完美匹配。 Any advise would be greatly appreciated. 任何建议将不胜感激。

The default minimum number of threads is the number of cores your machine has. 默认的最小线程数是您的计算机具有的核心数。 That's a good number, it doesn't generally make sense to run more threads than you have cores. 这是一个很好的数字,运行更多的线程通常没有核心。

The default maximum number of threads is 250 times the number of cores you have on .NET 2.0 SP1 and up. 默认的最大线程数是.NET 2.0 SP1及更高版本的核心数的250倍。 There is an enormous amount of breathing room here. 这里有很大的喘息空间。 On a four core machine, it would take 499 seconds to reach that maximum if none of the threads complete in a reasonable amount of time. 在四核机器上,如果没有一个线程在合理的时间内完成,则需要499秒才能达到最大值。

The threadpool scheduler tries to limit the number of active threads to the minimum, by default the number of cores you have. 线程池调度程序尝试将活动线程数限制为最小值,默认情况下是您拥有的核心数。 Twice a second it allows one more thread to start if the active threads do not complete. 如果活动线程没有完成,它会再次启动一个线程。 Threads that run for a very long time or do a lot of blocking that is not caused by I/O are not good candidates for the threadpool. 运行很长时间或执行大量阻塞但不是由I / O引起的线程不适合线程池。 You should use a regular Thread instead. 您应该使用常规线程。

Getting to the maximum isn't healthy. 达到最大值并不健康。 On a four core machine, just the stacks of those threads will consume a gigabyte of virtual memory space. 在四核计算机上,只需这些线程的堆栈就会消耗一千兆字节的虚拟内存空间。 Getting OOM is very likely. 很有可能获得OOM。 Consider lowering the max number of threads if that's your problem. 如果这是您的问题,请考虑降低最大线程数。 Or consider starting just a few regular Threads that receive packets of work from a thread-safe queue. 或者考虑从一个线程安全队列中开始接收一些工作包的常规线程。

Typically, the magic number is to leave it alone. 通常,神奇的数字是不管它。 The ThreadPool does a good job of handling this. ThreadPool可以很好地处理这个问题。

That being said, if you're doing a lot of long running services, and those services will have large periods where they're waiting, you may want to increase the maximum threads to handle more options. 话虽这么说,如果你正在做很多长时间运行的服务, 并且那些服务将有很长的等待时间,你可能想要增加最大线程来处理更多选项。 (If the processes aren't blocking, you'll probably just slow things down if you increase the thread count...) (如果进程没有阻塞,那么如果增加线程数,你可能只会减慢速度......)

Profile your application to find the correct number. 分析您的应用程序以找到正确的数字。

If you want better control, you might want to consider NOT using the built-in ThreadPool. 如果想要更好的控制,可能需要考虑不使用内置的ThreadPool。 There is a nice replacement at http://www.codeproject.com/KB/threads/smartthreadpool.aspx . http://www.codeproject.com/KB/threads/smartthreadpool.aspx上有一个很好的替代品。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用准则:C#中的ThreadPool.SetMaxThreads和ThreadPool.SetMinThreads - Guidelines while using: ThreadPool.SetMaxThreads and ThreadPool.SetMinThreads in C# ThreadPool.SetMinThreads()是否可以将completionPortThreads编号设置为0? - ThreadPool.SetMinThreads() can the completionPortThreads number be set to 0? ThreadPool SetMinThreads-设置它的影响 - ThreadPool SetMinThreads - the impact of setting it threadPool.SetMaxThreads返回false - threadPool.SetMaxThreads returning false System.Threading.ThreadPool.SetMaxThreads的默认值 - Default values for System.Threading.ThreadPool.SetMaxThreads ThreadPool 挫折 - 线程创建超过 SetMaxThreads - ThreadPool frustrations - Thread creation exceeding SetMaxThreads 仅在ThreadPool.SetMaxThreads c#中设置最大工作线程 - Set only maximum worker threads in ThreadPool.SetMaxThreads c# 我应该在什么时间和上下文中调用ThreadPool.SetMinThreads - At which time and in which context should I call ThreadPool.SetMinThreads 我正在实施作业队列。 使用TPL相同。 尝试使用ThreadPool.SetMaxThreads一次限制作业数量。 没运气 - I am working on implementing job queue. Using TPL for the same. Trying to restrict number of jobs at a time using ThreadPool.SetMaxThreads. no luck ThreadPool.SetMaxThreads不会阻止手动创建更多线程吗? - ThreadPool.SetMaxThreads does not prevent creating more Threads manually?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM