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

範例

下列範例會將背景工作執行緒數目下限設定為四個,並保留非同步 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) 取得, SetMinThreads 則會傳回 false ,而且不會變更其中一個最小值。

根據預設,執行緒數目下限會設定為處理器計數。 您可以使用 SetMinThreads 來增加最少的執行緒數目,例如暫時解決某些佇列工作專案或工作封鎖執行緒集區執行緒的問題。 這些封鎖有時會造成所有背景工作角色或 I/O 完成執行緒遭到封鎖的情況, () 。 不過,增加執行緒數目下限可能會以其他方式降低效能,例如:

  • 即使背景工作執行緒未遭到封鎖,執行緒集區也可以排程更多的背景工作執行緒。 超額訂閱可能會導致排程輸出的執行緒在等候長佇列中取得另一個時間配量時大幅延遲,並延遲某些工作專案或工作。
  • 背景工作執行緒可能需要更多 CPU 時間來取消佇列工作專案,因為必須掃描更多執行緒來竊取工作。
  • 執行緒之間的內容切換可能會增加 CPU 使用量。
  • 垃圾收集線上程堆疊逐步執行中可能需要更多 CPU 時間。
  • 進程可能會耗用更多記憶體。

警告

SetMinThreads使用 方法來增加最少的執行緒數目可能會導致效能問題,如上述文字中所述。 在大部分情況下,執行緒集區使用自己的演算法來配置執行緒時,效能會更好。 減少到小於處理器數目的最小值也會降低效能。

適用於

另請參閱