ThreadPool.SetMinThreads(Int32, Int32) 方法

定義

在切換至管理執行緒建立和解構的演算法之前,設定執行緒集區隨著提出新要求,視需要建立的執行緒最小數目。Sets the minimum number of threads the thread pool creates on demand, as new requests are made, before switching to an algorithm for managing thread creation and destruction.

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

執行緒集區視需要建立的背景工作執行緒最小數目。The minimum number of worker threads that the thread pool creates on demand.

completionPortThreads
Int32

執行緒集區視需要建立的非同步 I/O 執行緒最小數目。The minimum number of asynchronous I/O threads that the thread pool creates on demand.

傳回

Boolean

如果變更成功,則為 true;否則為 falsetrue if the change is successful; otherwise, false.

範例

下列範例會將背景工作執行緒的最小數目設定為4,並保留非同步 i/o 完成執行緒的最小數目的原始值。The following example sets the minimum number of worker threads to four, and preserves the original value for the minimum number of asynchronous I/O completion threads.

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 完成執行緒,直到達到每個類別的最小值為止。The thread pool provides new worker threads or I/O completion threads on demand until it reaches the minimum for each category. 達到最小值時,執行緒集區可以在該類別中建立額外的執行緒,或等候部分工作完成。When the minimum is reached, the thread pool can create additional threads in that category or wait until some tasks complete. 從開始 .NET Framework 4.NET Framework 4 ,執行緒集區會建立和終結執行緒,以將輸送量優化,這定義為每個時間單位完成的工作數目。Beginning with the .NET Framework 4.NET Framework 4, the thread pool creates and destroys threads in order to optimize throughput, which is defined as the number of tasks that complete per unit of time. 執行緒太少可能無法最有效地利用可用資源,而執行緒太多則可能增加資源爭用的情況。Too few threads might not make optimal use of available resources, whereas too many threads could increase resource contention.

當需求很低時,執行緒集區執行緒的實際數目可能低於最小值。When demand is low, the actual number of thread pool threads can fall below the minimum values.

如果您指定負數或大於使用) 所取得 (使用中線程集區執行緒數目上限的數位,則會傳回, GetMaxThreads SetMinThreads false 而且不會變更其中一個最小值。If you specify a negative number or a number larger than the maximum number of active thread pool threads (obtained using GetMaxThreads), SetMinThreads returns false and does not change either of the minimum values.

警告

根據預設,執行緒的數目下限設為系統上的處理器數目。By default, the minimum number of threads is set to the number of processors on a system. 您可以使用 SetMinThreads 方法來增加執行緒的最小數目。You can use the SetMinThreads method to increase the minimum number of threads. 不過,不必要地增加這些值,可能會造成效能問題。However, unnecessarily increasing these values can cause performance problems. 如果太多工作同時啟動,則所有工作可能都會變慢。If too many tasks start at the same time, all of them might appear to be slow. 在大部分的情況下,執行緒集區會使用自己的演算法來配置執行緒,以執行得更好。In most cases, the thread pool will perform better with its own algorithm for allocating threads. 將最小值減少到小於處理器數目也可能會影響效能。Reducing the minimum to less than the number of processors can also hurt performance.

適用於

另請參閱