C# ThreadPool SetMaxThreads 和 SetMinThreads 幻数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2043909/
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
ThreadPool SetMaxThreads and SetMinThreads Magic Number
提问by Benny
Is there a magic number or formula for setting the values of SetMaxThreads and SetMinThreads for ThreadPool? 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.
是否有用于设置 ThreadPool 的 SetMaxThreads 和 SetMinThreads 值的幻数或公式?我有数千个需要执行的长时间运行的方法,但找不到设置这些值的完美匹配。任何建议将不胜感激。
采纳答案by Hans Passant
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. 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.
默认的最大线程数是 .NET 2.0 SP1 及更高版本上的内核数的 250 倍。这里有巨大的喘息空间。在四核机器上,如果没有一个线程在合理的时间内完成,则需要 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. You should use a regular Thread instead.
线程池调度程序尝试将活动线程的数量限制为最小值,默认情况下为您拥有的内核数量。如果活动线程未完成,则每秒两次它允许再启动一个线程。运行很长时间或执行大量非 I/O 引起的阻塞的线程不适合线程池。您应该改用常规线程。
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. 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.
达到最大值是不健康的。在四核机器上,仅这些线程的堆栈将消耗 1 GB 的虚拟内存空间。很可能出现OOM。如果这是您的问题,请考虑降低最大线程数。或者考虑只启动几个从线程安全队列接收工作包的常规线程。
回答by Reed Copsey
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, andthose 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.
分析您的应用程序以找到正确的编号。
回答by Michael Bray
If you want better control, you might want to consider NOT using the built-in ThreadPool. There is a nice replacement at http://www.codeproject.com/KB/threads/smartthreadpool.aspx.
如果您想要更好的控制,您可能需要考虑不使用内置的 ThreadPool。http://www.codeproject.com/KB/threads/smartthreadpool.aspx 上有一个很好的替代品。