다음을 통해 공유


ThreadPool 클래스

작업 항목 게시, 비동기 I/O 처리, 다른 스레드 대신 기다리기 및 타이머 처리에 사용할 수 있는 스레드 풀을 제공합니다.

네임스페이스: System.Threading
어셈블리: mscorlib(mscorlib.dll)

구문

‘선언
Public NotInheritable Class ThreadPool
‘사용 방법
정적 클래스의 멤버는 클래스 인스턴스를 사용하지 않고 직접 액세스할 수 있습니다.
public static class ThreadPool
public ref class ThreadPool abstract sealed
public final class ThreadPool
public final class ThreadPool

설명

참고

이 클래스에 적용되는 HostProtectionAttribute 특성의 Resources 속성 값은 Synchronization | ExternalThreading입니다. HostProtectionAttribute는 대개 아이콘을 두 번 클릭하거나, 명령을 입력하거나, 브라우저에서 URL을 입력하여 시작되는 데스크톱 응용 프로그램에 영향을 미치지 않습니다. 자세한 내용은 HostProtectionAttribute 클래스나 SQL Server 프로그래밍 및 호스트 보호 특성을 참조하십시오.

대부분의 응용 프로그램에서는 이벤트가 발생하기를 기다리며 많은 시간을 대기 상태에서 보내는 스레드를 만듭니다. 어떤 스레드는 대기 상태에 있다가 변경 또는 업데이트 상태 정보를 폴링하기 위해 정기적으로 깨어납니다. 스레드 풀링을 사용하면 시스템에서 관리하는 작업자 스레드의 풀을 응용 프로그램에 제공하여 스레드를 좀 더 효율적으로 사용할 수 있습니다. 한 스레드는 스레드 풀에 대기 중인 여러 대기 작업의 상태를 모니터링합니다. 대기 작업이 완료되면 스레드 풀의 작업자 스레드가 해당 콜백 함수를 실행합니다.

참고

관리되는 스레드 풀에 있는 스레드는 백그라운드 스레드입니다. 즉, IsBackground 속성이 true입니다. 이것은 모든 포그라운드 스레드가 종료된 후 ThreadPool 스레드에서 응용 프로그램을 계속 실행하지 않을 것임을 의미합니다.

또, 대기 작업과 관련되지 않은 작업 항목을 스레드 풀에 대기시킬 수 있습니다. 스레드 풀에 있는 스레드에서 작업 항목을 처리하도록 요청하려면 QueueUserWorkItem 메서드를 호출하십시오. 이 메서드는 스레드 풀에서 선택한 스레드가 호출한 메서드 또는 대리자에 대한 참조를 매개 변수로 사용합니다. 작업 항목을 큐에 대기시킨 후에는 취소할 수 없습니다.

타이머 큐 타이머와 등록된 대기 작업도 스레드 풀을 사용합니다. 타이머 큐 타이머와 등록된 대기 작업의 콜백 함수는 스레드 풀에 대기합니다.

프로세스마다 스레드 풀이 하나씩 있습니다. 스레드 풀의 기본 크기는 사용 가능한 프로세서 당 25개의 스레드입니다. 스레드 풀의 스레드 수는 SetMaxThreads 메서드를 사용하여 변경할 수 있습니다. 각 스레드는 기본 스택 크기를 사용하며 기본 우선 순위에서 실행됩니다.

참고

.NET Framework를 호스팅하는 비관리 코드는 mscoree.h 파일에 정의된 CorSetMaxThreads 함수를 사용하여 스레드 풀의 크기를 변경할 수 있습니다.

항목 위치
방법: 비동기 HTTP 처리기 만들기 Visual Studio에서 ASP .NET 웹 응용 프로그램 빌드
방법: 비동기 HTTP 처리기 만들기 Visual Studio에서 ASP .NET 웹 응용 프로그램 빌드
방법: 비동기 HTTP 처리기 만들기 Building ASP .NET Web Applications

예제

Imports System
Imports System.Threading

Public Class Example

    <MTAThread> _
    Public Shared Sub Main()
        ' Queue the task.
        ThreadPool.QueueUserWorkItem( _
            New WaitCallback(AddressOf ThreadProc) _
            )
        ' Note that you do not have to create the WaitCallback delegate
        ' explicitly in Visual Basic.  The following line also queues 
        ' the task:
        'ThreadPool.QueueUserWorkItem(AddressOf ThreadProc)
        
        Console.WriteLine("Main thread does some work, then sleeps.")
        ' If you comment out the Sleep, the main thread exits before
        ' the thread pool task runs.  The thread pool uses background
        ' threads, which do not keep the application running.  (This
        ' is a simple example of a race condition.)
        Thread.Sleep(1000)

        Console.WriteLine("Main thread exits.")
    End Sub

    ' This thread procedure performs the task.
    Shared Sub ThreadProc(stateInfo As Object)
        ' No state object was passed to QueueUserWorkItem, so 
        ' stateInfo is null.
        Console.WriteLine("Hello from the thread pool.")
    End Sub
End Class
using System;
using System.Threading;
public class Example {
    public static void Main() {
        // Queue the task.
        ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc));
        
        Console.WriteLine("Main thread does some work, then sleeps.");
        // If you comment out the Sleep, the main thread exits before
        // the thread pool task runs.  The thread pool uses background
        // threads, which do not keep the application running.  (This
        // is a simple example of a race condition.)
        Thread.Sleep(1000);

        Console.WriteLine("Main thread exits.");
    }

    // This thread procedure performs the task.
    static void ThreadProc(Object stateInfo) {
        // No state object was passed to QueueUserWorkItem, so 
        // stateInfo is null.
        Console.WriteLine("Hello from the thread pool.");
    }
}
using namespace System;
using namespace System::Threading;
ref class Example
{
public:

   // This thread procedure performs the task.
   static void ThreadProc( Object^ stateInfo )
   {
      
      // No state object was passed to QueueUserWorkItem, so 
      // stateInfo is 0.
      Console::WriteLine( "Hello from the thread pool." );
   }

};

int main()
{
   
   // Queue the task.
   ThreadPool::QueueUserWorkItem( gcnew WaitCallback( Example::ThreadProc ) );
   Console::WriteLine( "Main thread does some work, then sleeps." );
   
   // If you comment out the Sleep, the main thread exits before
   // the thread pool task runs.  The thread pool uses background
   // threads, which do not keep the application running.  (This
   // is a simple example of a race condition.)
   Thread::Sleep( 1000 );
   Console::WriteLine( "Main thread exits." );
   return 0;
}
import System.*;
import System.Threading.*;
import System.Threading.Thread;

public class Example
{
    public static void main(String[] args)
    {
        // Queue the task.
        ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc));
        Console.WriteLine("Main thread does some work, then sleeps.");

        // If you comment out the Sleep, the main thread exits before
        // the thread pool task runs.  The thread pool uses background
        // threads, which do not keep the application running.  (This
        // is a simple example of a race condition.)
        Thread.Sleep(1000);
        Console.WriteLine("Main thread exits.");
    } //main

    // This thread procedure performs the task.
    static void ThreadProc(Object stateInfo)
    {
        // No state object was passed to QueueUserWorkItem, so 
        // stateInfo is null.
        Console.WriteLine("Hello from the thread pool.");
    } //ThreadProc
} //Example

상속 계층 구조

System.Object
  System.Threading.ThreadPool

스레드로부터의 안전성

이 형식의 모든 public static(Visual Basic의 경우 Shared) 멤버는 스레드로부터 안전합니다. 인터페이스 멤버는 스레드로부터 안전하지 않습니다.

플랫폼

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework에서 모든 플래폼의 모든 버전을 지원하지는 않습니다. 지원되는 버전의 목록은 시스템 요구 사항을 참조하십시오.

버전 정보

.NET Framework

2.0, 1.1, 1.0에서 지원

.NET Compact Framework

2.0, 1.0에서 지원

참고 항목

참조

ThreadPool 멤버
System.Threading 네임스페이스

기타 리소스

스레드 및 스레딩
관리되는 스레드 풀