ThreadPool.SetMinThreads(Int32, Int32) 方法

定义

发出新的请求时,在切换到管理线程创建和销毁的算法之前设置线程池按需创建的线程的最小数量。

public:
 static bool SetMinThreads(int workerThreads, int completionPortThreads);
public static bool SetMinThreads (int workerThreads, int completionPortThreads);
static member SetMinThreads : int * int -> bool
Public Shared Function SetMinThreads (workerThreads As Integer, completionPortThreads As Integer) As Boolean

参数

workerThreads
Int32

要由线程池根据需要创建的新的最小工作程序线程数。

completionPortThreads
Int32

要由线程池根据需要创建的新的最小空闲异步 I/O 线程数。

返回

如果更改成功,则为 true;否则为 false

示例

以下示例将最小工作线程数设置为 4,并保留最小异步 I/O 完成线程数的原始值。

using namespace System;
using namespace System::Threading;
int main()
{
   int minWorker;
   int minIOC;
   
   // Get the current settings.
   ThreadPool::GetMinThreads( minWorker, minIOC );
   
   // Change the minimum number of worker threads to four, but
   // keep the old setting for minimum asynchronous I/O
   // completion threads.
   if ( ThreadPool::SetMinThreads( 4, minIOC ) )
   {
      
      // The minimum number of threads was set successfully.
   }
   else
   {
      
      // The minimum number of threads was not changed.
   }
}
using System;
using System.Threading;

public class Test
{
    public static void Main()
    {
        int minWorker, minIOC;
        // Get the current settings.
        ThreadPool.GetMinThreads(out minWorker, out minIOC);
        // Change the minimum number of worker threads to four, but
        // keep the old setting for minimum asynchronous I/O 
        // completion threads.
        if (ThreadPool.SetMinThreads(4, minIOC))
        {
            // The minimum number of threads was set successfully.
        }
        else
        {
            // The minimum number of threads was not changed.
        }
    }
}
Imports System.Threading

Public Class Test

    <MTAThread> _
    Public Shared Sub Main()
        Dim minWorker, minIOC As Integer
        ' Get the current settings.
        ThreadPool.GetMinThreads(minWorker, minIOC)
        ' Change the minimum number of worker threads to four, but
        ' keep the old setting for minimum asynchronous I/O 
        ' completion threads.
        If ThreadPool.SetMinThreads(4, minIOC) Then
            ' The minimum number of threads was set successfully.
        Else
            ' The minimum number of threads was not changed.
        End If
    End Sub
End Class

注解

将 Windows 线程池配置为使用 而不是 .NET 线程池时,不支持此方法。 有关详细信息,请参阅 Windows 线程池配置设置

线程池按需提供新的工作线程或 I/O 完成线程,直到达到每个类别的最小值。 达到最小值时,线程池可以在该类别中创建其他线程,或等到某些任务完成。 从.NET Framework 4 开始,线程池创建和销毁线程以优化吞吐量,吞吐量定义为每个时间单位完成的任务数。 线程过少可能无法实现可用资源的最优利用,而线程过多则可能增加资源争用。

需求较低时,线程池线程的实际数量可以低于最小值。

如果指定负数或大于 (使用 GetMaxThreads) 获取的活动线程池线程的最大数目, SetMinThreadsfalse 返回 且 不会更改最小值之一。

默认情况下,最小线程数设置为处理器计数。 可以使用 SetMinThreads 来增加最小线程数,例如暂时解决某些排队工作项或任务阻止线程池线程的问题。 这些阻塞有时会导致所有辅助角色或 I/O 完成线程被阻塞 () 。 但是,增加最小线程数可能会以其他方式降低性能,例如:

  • 即使未阻止工作线程,线程池也可能计划更多工作线程。 过度订阅可能会导致计划退出的线程在等待长队列以获取另一个时间切片时明显延迟,从而延迟某些工作项或任务。
  • 由于必须扫描更多线程来窃取工作,工作线程在取消排队工作项时可能需要更多 CPU 时间。
  • 线程之间的上下文切换可能会增加 CPU 使用率。
  • 在线程堆栈浏览中,垃圾回收可能需要更多 CPU 时间。
  • 进程可能会消耗更多内存。

注意

SetMinThreads使用 方法增加最小线程数可能会导致性能问题,如前面的文本中所述。 在大多数情况下,线程池使用自己的算法分配线程时性能更好。 将最小值减少到小于处理器数也会损害性能。

适用于

另请参阅