ThreadPool SetMinThreads - 设置它的影响 [英] ThreadPool SetMinThreads - the impact of setting it

查看:17
本文介绍了ThreadPool SetMinThreads - 设置它的影响的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解设置 ThreadPool.SetMinthreads 的影响.我有多个虚拟应用程序在一个 Azure 应用服务中运行.我的理解是,所有这些虚拟应用程序都将共享 App Pool,并且只有一个工作进程(假设 App Pool 的最大工作进程为 1).

我有以下两个问题.

  1. 在此设置中,如果我将 ThreadPool.SetMinThreads 设置为 100 个工作线程和 IO 线程,我可以安全地假设每个应用程序域将有 100 个工作线程和 100 个 IO 线程加载?确切地说,ThreadPool.SetMinThreads 适用于 AppDomain,还是 Worker Process 或 App Pool?ThreadPool 的范围是什么?
  2. 我还假设系统可以生成的最大线程数没有限制,因为它由底层主机的容量决定.这意味着,如果我没有显式设置 ThreadPool.SetMaxThreads,系统将生成新线程,并且如果持续负载直到 CPU/内存达到最大值,系统将继续执行此操作.我基于以下陈述来支持我的假设:

<块引用>

例如,进程和线程需要物理内存、虚拟内存内存和池内存,因此进程或线程的数量能否在给定的 Windows 系统上创建最终取决于这些资源之一,取决于流程或创建线程并首先命中哪个约束.https://blogs.technet.microsoft.com/markrussinovich/2009/07/05/pushing-the-limits-of-windows-processes-and-threads/

解决方案

MinThreads 控制将产生多少工作线程没有延迟.

每当你做某事需要线程池中的一个线程(无论是worker还是IOCP池),系统都会首先查看是否有空闲线程.

如果没有,它会查看当前产生了多少线程.如果该数字小于 MinThreads,它会立即产生一个新线程.否则它会等待很短的时间,通常在 300-500 毫秒左右,尽管这取决于系统.如果仍然没有空闲线程,它将产生一个新线程.

当然,这一切仍然受到 MaxThreads 的限制.

话虽如此,IIS 非常擅长根据您的机器计算出一个合理的数字,在大多数情况下,您最好不要管它;如果您只是担心服务请求,那么我不会亲自触及它.另一方面,如果您自己产生了很多后台任务,那么这可能是明智的.我强烈建议您在实际进行更改之前对其进行测量.

虽然...将 MinThreads 设置为 100 很少有害,尤其是因为系统只会启动它实际需要的线程数

I am trying to understand the impact of setting ThreadPool.SetMinthreads. I have multiple virtual applications running in one Azure App Service. My understanding is that all these virtual applications will share the App Pool, and will have only one worker process (Assuming the App Pool's max worker process will be 1).

I have the below two questions.

  1. In this setup, if I set ThreadPool.SetMinThreads to let's say 100 worker threads and IO threads, can I safely assume that each app domain will have 100 worker threads and 100 IO threads when it is loaded? To be precise, the ThreadPool.SetMinThreads applies within the AppDomain, or Worker Process or App Pool? What is the scope of ThreadPool?
  2. I also assume there is no limitation on the max threads the system can spawn as it is determined by the underlying host's capacity. This means, if I do not explicitly set ThreadPool.SetMaxThreads, the system will spawn new threads and will continue to do it if there is a continuous load till CPU/Memory spikes to the max. I am basing on the below statement to support my assumption:

Process and threads, for example, require physical memory, virtual memory, and pool memory, so the number of processes or threads that can be created on a given Windows system is ultimately determined by one of these resources, depending on the way that the processes or threads are created and which constraint is hit first. https://blogs.technet.microsoft.com/markrussinovich/2009/07/05/pushing-the-limits-of-windows-processes-and-threads/

解决方案

The MinThreads governs how many worker threads will be spawned without a delay.

Whenever you do something that requires a thread from the thread pool (whether worker or IOCP pool), the system will first see if there is a free thread.

If not, it looks to see how many threads are currently spawned. If that number is less than MinThreads, it immediately spawns a new thread. Otherwise it waits a short time, usually around 300-500ms, though that is system dependent. If there is still no free thread, it will then spawn a new thread.

Of course, this all is still limited by MaxThreads.

All that said, IIS is very good at figuring out a sensible number based on your machine and in most cases you are best to leave it alone; if you are just worried about serving requests then I wouldn't touch it personally. If, on the other hand, you are spawning a lot of background tasks yourself then it may be sensible. I'd strongly encourage you to measure it before you actually make changes.

Though... Setting MinThreads to 100 is rarely harmful, especially as the system will only start the number of threads it actually needs anyway

这篇关于ThreadPool SetMinThreads - 设置它的影响的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆