ThreadPool SetMinThreads - 设置它的影响

ThreadPool SetMinThreads - the impact of setting it

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

我有以下两个问题。

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

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/

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

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

如果不是,它会查看当前生成了多少线程。如果该数字小于 MinThreads,它会立即生成一个新线程。否则它会等待很短的时间,通常大约 300-500 毫秒,但这取决于系统。如果仍然没有空闲线程,它将生成一个新线程。

当然,这一切还是受MaxThreads的限制。

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

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