ThreadPool.GetMinThreads(Int32, Int32) 方法

定義

在切換至管理執行緒建立和解構的演算法之前,擷取執行緒集區隨著提出新要求,視需要建立的執行緒最小數目。

public static void GetMinThreads(out int workerThreads, out int completionPortThreads);

參數

workerThreads
Int32

當這個方法傳回時,會包含執行緒集區視需要建立的背景工作執行緒最小數目。

completionPortThreads
Int32

當這個方法傳回時,會包含執行緒集區視需要建立的非同步 I/O 執行緒最小數目。

範例

下列範例會將背景工作執行緒數目下限設定為四個,並保留非同步 I/O 完成執行緒數目下限的原始值。

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.
        }
    }
}

備註

執行緒集區會視需要提供新的背景工作執行緒或 I/O 完成執行緒,直到達到每個類別的最小值為止。 根據預設,執行緒的數目下限設為系統上的處理器數目。 達到最小值時,執行緒集區可以在該類別中建立其他執行緒,或等到某些工作完成為止。 從.NET Framework 4 開始,執行緒集區會建立和終結執行緒,以優化輸送量,其定義為每個單位時間完成的工作數目。 執行緒太少可能無法最有效地利用可用資源,而執行緒太多則可能增加資源爭用的情況。

注意

當需求很低時,執行緒集區執行緒的實際數目可能低於最小值。

適用於

產品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

另請參閱