ThreadPool クラスとは? わかりやすく解説

ThreadPool クラス

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

名前空間: System.Threading
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

解説解説
メモメモ

このクラス適用される HostProtectionAttribute 属性Resources プロパティの値は、Synchronization または ExternalThreading です。HostProtectionAttribute は、デスクトップ アプリケーション (一般的にはアイコンダブルクリックコマンド入力、またはブラウザURL入力して起動するアプリケーション) には影響しません。詳細については、HostProtectionAttribute クラストピックまたは「SQL Server プログラミングホスト保護属性」を参照してください

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

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

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

プロセスごとに、1 つスレッド プールありますスレッド プール既定サイズは、利用できるプロセッサごとに 25 個のスレッドです。スレッド プール内のスレッドの数は、SetMaxThreads メソッド使用して変更できます。各スレッドは、既定スタック サイズ使用し既定優先順位実行されます。

使用例使用例
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) メンバはすべて、スレッド セーフです。インスタンス メンバ場合は、スレッド セーフであるとは限りません。
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照


このページでは「.NET Framework クラス ライブラリ リファレンス」からThreadPool クラスを検索した結果を表示しています。
Weblioに収録されているすべての辞書からThreadPool クラスを検索する場合は、下記のリンクをクリックしてください。
 全ての辞書からThreadPool クラス を検索

英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「ThreadPool クラス」の関連用語

ThreadPool クラスのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



ThreadPool クラスのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2025 Microsoft.All rights reserved.

©2025 GRAS Group, Inc.RSS