ThreadPool クラス

作業項目の送信、非同期 I/O の処理、他のスレッドの代理で行う待機、およびタイマの処理に使用できるスレッドのプールを提供します。

この型のすべてのメンバの一覧については、ThreadPool メンバ を参照してください。

System.Object
   System.Threading.ThreadPool

NotInheritable Public Class ThreadPool
[C#]
public sealed class ThreadPool
[C++]
public __gc __sealed class ThreadPool
[JScript]
public class ThreadPool

スレッドセーフ

この型の public static (Visual Basicでは Shared) のすべてのメンバは、マルチスレッド操作で安全に使用できます。インスタンスのメンバの場合は、スレッドセーフであるとは限りません。

解説

多くのアプリケーションが、イベント発生に備えて待機する時間の長いスレッド、つまりスリープ状態のスレッドを作成します。他のスレッドはスリープ状態に入り、ステータス情報の変更や更新がないかポーリングを行うサイクルのときの定期的に起動されます。スレッド プールを使用すると、システムで管理されるワーカー スレッドのプールをアプリケーションに提供することで、スレッドをより効率的に使用できます。1 つのスレッドが、スレッド プールのキューに置かれた複数の待機操作のステータスを監視します。待機操作が完了すると、スレッド プールのワーカー スレッドは該当するコールバック関数を実行します。

待機操作に関連のない作業項目をスレッド プールのキューに置くこともできます。作業項目をスレッド プール内のスレッドで処理することを要求するには、 QueueUserWorkItem メソッドを呼び出します。このメソッドは、スレッド プールから選択されたスレッドによって呼び出されるメソッドまたはデリゲートへの参照をパラメータとして取得します。キューに置かれた作業項目はキャンセルできません。

タイマ待ちタイマおよび登録された待機操作もスレッド プールを使用します。これらのコールバック関数は、スレッド プールのキューに置かれます。

スレッド プールは、 ThreadPool クラスのインスタンスを初めて作成したときに作成されます。スレッド プールには、利用できるプロセッサごとに 25 個のスレッドという既定の制限があります。この制限は mscoree.h ファイルで定義されている CorSetMaxThreads を使用して変更できます。各スレッドは、既定のスタック サイズを使用し、既定の優先順位で実行されます。プロセスごとに、1 つのオペレーティング システム スレッド プールだけを持つことができます。

使用例

 
Imports System
Imports System.Threading

Public Class Example
    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

[C#] 
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.");
    }
}

[C++] 
#using <mscorlib.dll>

using namespace System;
using namespace System::Threading;

__gc 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(S"Hello from the thread pool.");
    }
};

int main() 
{
    // Queue the task.
    ThreadPool::QueueUserWorkItem(new WaitCallback(0, Example::ThreadProc));

    Console::WriteLine(S"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(S"Main thread exits.");
    return 0;
}

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

名前空間: System.Threading

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET

アセンブリ: Mscorlib (Mscorlib.dll 内)

参照

ThreadPool メンバ | System.Threading 名前空間 | スレッドおよびスレッド処理 | スレッド プーリング