ThreadPool.GetMinThreads(Int32, Int32) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
在切換至管理執行緒建立和解構的演算法之前,擷取執行緒集區隨著提出新要求,視需要建立的執行緒最小數目。
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 執行緒最小數目。
範例
下列範例會將背景工作執行緒數目下限設定為四個,並保留非同步 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 開始,執行緒集區會建立和終結執行緒,以優化輸送量,其定義為每個單位時間完成的工作數目。 執行緒太少可能無法最有效地利用可用資源,而執行緒太多則可能增加資源爭用的情況。
注意
當需求很低時,執行緒集區執行緒的實際數目可能低於最小值。