다음을 통해 공유


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입니다.

예제

다음 예제에서는 최소 작업자 스레드 수를 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

설명

Windows 스레드 풀이 .NET 스레드 풀 대신 사용하도록 구성된 경우에는 이 메서드가 지원되지 않습니다. 자세한 내용은 Windows 스레드 풀 구성 설정을 참조하세요.

스레드 풀은 각 범주에 대한 최소값에 도달할 때까지 요청 시 새 작업자 스레드 또는 I/O 완료 스레드를 제공합니다. 최소값에 도달하면 스레드 풀은 해당 범주에 추가 스레드를 만들거나 일부 작업이 완료될 때까지 기다릴 수 있습니다. .NET Framework 4부터 스레드 풀은 처리량을 최적화하기 위해 스레드를 만들고 삭제합니다. 이 스레드는 시간 단위당 완료되는 작업 수로 정의됩니다. 스레드가 너무 적으면 사용 가능한 리소스가 효율적으로 사용되지 않는 반면, 너무 많으면 리소스 경합이 증가할 수 있습니다.

요구가 적을 때는 실제 스레드 풀 스레드 수가 최소값보다 작을 수 있습니다.

음수 또는 활성 스레드 풀 스레드의 최대 수(를 사용하여 GetMaxThreadsSetMinThreads 얻은)보다 큰 숫자를 지정하면 은 를 반환 false 하고 최소값 중 하나를 변경하지 않습니다.

기본적으로 최소 스레드 수는 프로세서 수로 설정됩니다. 를 사용하여 SetMinThreads 대기 중인 일부 작업 항목 또는 태스크가 스레드 풀 스레드를 차단하는 문제를 일시적으로 해결하는 것과 같은 최소 스레드 수를 늘릴 수 있습니다. 이러한 막힘은 경우에 따라 모든 작업자 또는 I/O 완료 스레드가 차단되는 상황(고갈)으로 이어질 수 있습니다. 그러나 최소 스레드 수를 늘리면 다음과 같은 다른 방법으로 성능이 저하될 수 있습니다.

  • 스레드 풀은 작업자 스레드가 차단되지 않는 경우에도 더 많은 작업자 스레드를 예약할 수 있습니다. 초과 구독으로 인해 예약된 스레드가 긴 큐에서 대기하여 다른 시간 조각을 가져오기를 기다리는 동안 크게 지연되어 일부 작업 항목 또는 작업이 지연될 수 있습니다.
  • 작업자 스레드는 작업을 훔치기 위해 더 많은 스레드를 검색해야 하여 작업 항목을 큐에서 해제하는 데 더 많은 CPU 시간이 걸릴 수 있습니다.
  • 스레드 간에 컨텍스트를 전환하면 CPU 사용량이 증가할 수 있습니다.
  • 가비지 수집은 스레드 스택 보행에 더 많은 CPU 시간이 걸릴 수 있습니다.
  • 프로세스에서 더 많은 메모리를 사용할 수 있습니다.

주의

메서드를 SetMinThreads 사용하여 최소 스레드 수를 늘리면 앞의 텍스트에 설명된 대로 성능 문제가 발생할 수 있습니다. 대부분의 경우 스레드 풀은 스레드를 할당하기 위한 자체 알고리즘을 사용하여 더 나은 성능을 발휘합니다. 최소를 프로세서 수보다 작게 줄이면 성능이 저하될 수 있습니다.

적용 대상

추가 정보