ThreadPool.GetMinThreads(Int32, Int32) 方法

定义

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

public:
 static void GetMinThreads([Runtime::InteropServices::Out] int % workerThreads, [Runtime::InteropServices::Out] int % completionPortThreads);
public static void GetMinThreads (out int workerThreads, out int completionPortThreads);
static member GetMinThreads : int * int -> unit
Public Shared Sub GetMinThreads (ByRef workerThreads As Integer, ByRef completionPortThreads As Integer)

参数

workerThreads
Int32

当此方法返回时,将包含线程池根据需要创建的最少数量的辅助线程。

completionPortThreads
Int32

当此方法返回时,将包含线程池根据需要创建的最少数量的异步 I/O 线程。

示例

以下示例将最小工作线程数设置为 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

注解

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

注意

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

适用于

另请参阅