ThreadPool.GetAvailableThreads メソッドとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > ThreadPool.GetAvailableThreads メソッドの意味・解説 

ThreadPool.GetAvailableThreads メソッド

スレッド プール スレッド最大数 (GetMaxThreads から返される) と現在アクティブスレッドの数との差を取得します

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

Public Shared Sub GetAvailableThreads
 ( _
    <OutAttribute> ByRef workerThreads As
 Integer, _
    <OutAttribute> ByRef completionPortThreads As
 Integer _
)
Dim workerThreads As Integer
Dim completionPortThreads As Integer

ThreadPool.GetAvailableThreads(workerThreads, completionPortThreads)
public static void GetAvailableThreads
 (
    out int workerThreads,
    out int completionPortThreads
)
public:
static void GetAvailableThreads (
    [OutAttribute] int% workerThreads, 
    [OutAttribute] int% completionPortThreads
)
public static void GetAvailableThreads
 (
    /** @attribute OutAttribute() */ /** @ref */ int workerThreads,
 
    /** @attribute OutAttribute() */ /** @ref */ int completionPortThreads
)
JScript では、値型引数参照渡しされません。

パラメータ

workerThreads

使用できるワーカー スレッドの数。

completionPortThreads

使用できる非同期 I/O スレッドの数。

解説解説
使用例使用例

スレッド プール内の最大スレッド数と使用できるスレッド数のカウント取得する方法の例を次に示します。この例では、FileStream使用して 2 つファイル非同期的に書き込みを行うための作業項目がキュー置かれます。また、コールバック メソッドは、オーバーラップするように時間設定されます。作業項目はワーカー スレッドによって処理されコンピュータ上のプロセッサ速度と数に応じて1 つまたは 2 つ完了ポート スレッド書き込み操作処理します

Imports Microsoft.VisualBasic
Imports System
Imports System.IO
Imports System.Security.Permissions
Imports System.Threading

' Request permission to create two data files.
<Assembly: FileIOPermissionAttribute(SecurityAction.RequestMinimum, _
    All := "C:\Test1111.dat")>
<Assembly: FileIOPermissionAttribute(SecurityAction.RequestMinimum, _
    All := "C:\Test2222.dat")>

Public Class Test


    <MTAThread> _
    Shared Sub Main()
        Dim mainEvent As New
 AutoResetEvent(False)
        Dim workerThreads As Integer
 
        Dim portThreads As Integer
 

        ThreadPool.GetMaxThreads(workerThreads, portThreads)
        Console.WriteLine(vbCrLf & "Maximum worker threads:
 " & _
            vbTab & "{0}" & vbCrLf & "Maximum
 completion port " & _
            "threads: {1}", workerThreads, portThreads)

        ThreadPool.GetAvailableThreads(workerThreads, portThreads)
        Console.WriteLine(vbCrLf & "Available worker threads:
 " & _
            vbTab & "{0}" & vbCrLf & "Available
 completion port " & _
            "threads: {1}" & vbCrLf, workerThreads,
 portThreads)

        ThreadPool.QueueUserWorkItem(AddressOf _
            ThreadPoolTest.WorkItemMethod, mainEvent)
           
        ' Since ThreadPool threads are background threads, 
        ' wait for the work item to signal before ending Main.
        mainEvent.WaitOne(5000, False)
    End Sub

End Class

Public Class ThreadPoolTest

    ' Maintains state information to be passed to EndWriteCallback.
    ' This information allows the callback to end the asynchronous
    ' write operation and signal when it is finished.
    Class State
        Public fStream As FileStream
        Public autoEvent As AutoResetEvent

        Public Sub New(aFileStream
 As FileStream, anEvent As AutoResetEvent)
            fStream   = aFileStream
            autoEvent = anEvent
        End Sub
    End Class   
    
    Private Sub New
    End Sub

    Shared Sub WorkItemMethod(mainEvent As
 Object)
    
        Console.WriteLine(vbCrLf & "Starting WorkItem."
 & vbCrLf)
        Dim autoEvent As New
 AutoResetEvent(False)

        ' Create some data.
        Const ArraySize As Integer
  = 10000
        Const BufferSize As Integer
 =  1000
        Dim byteArray As Byte()
 = New Byte(ArraySize){}
        Dim randomGenerator As New
 Random()
        randomGenerator.NextBytes(byteArray)

        ' Create two files and two State objects. 
        Dim fileWriter1 As FileStream = _
            New FileStream("C:\Test1111.dat",
 FileMode.Create, _
            FileAccess.ReadWrite, FileShare.ReadWrite, _
            BufferSize, True)
        Dim fileWriter2 As FileStream = _
            New FileStream("C:\Test2222.dat",
 FileMode.Create, _
            FileAccess.ReadWrite, FileShare.ReadWrite, _
            BufferSize, True)
        Dim stateInfo1 As New
 State(fileWriter1, autoEvent)
        Dim stateInfo2 As New
 State(fileWriter2, autoEvent)

        ' Asynchronously write to the files.
        fileWriter1.BeginWrite(byteArray, 0, byteArray.Length, _
            AddressOf EndWriteCallback, stateInfo1)
        fileWriter2.BeginWrite(byteArray, 0, byteArray.Length, _
            AddressOf EndWriteCallback, stateInfo2)

        ' Wait for the callbacks to signal.
        autoEvent.WaitOne()
        autoEvent.WaitOne()

        fileWriter1.Close()
        fileWriter2.Close()
        Console.WriteLine(vbCrLf & "Ending WorkItem."
 & vbCrLf)

        ' Signal Main that the work item is finished.
        DirectCast(mainEvent, AutoResetEvent).Set()
    
    End Sub

    Shared Sub EndWriteCallback(asyncResult
 As IAsyncResult)
    
        Console.WriteLine("Starting EndWriteCallback.")

        Dim stateInfo As State = _
            DirectCast(asyncResult.AsyncState, State)
        Dim workerThreads As Integer
 
        Dim portThreads As Integer
 
        Try
            ThreadPool.GetAvailableThreads(workerThreads, portThreads)
            Console.WriteLine(vbCrLf & "Available worker "
 & _
                "threads:" & vbTab & "{0}"
 & vbCrLf & "Available " & _
                "completion port threads: {1}" &
 vbCrLf, _
                workerThreads, portThreads)

            stateInfo.fStream.EndWrite(asyncResult)

            ' Sleep so the other thread has a chance to run
            ' before the current thread ends.
            Thread.Sleep(1500)
        Finally
        
            ' Signal that the current thread is finished.
            stateInfo.autoEvent.Set()
            Console.WriteLine("Ending EndWriteCallback.")
        End Try
    
    End Sub
End Class
using System;
using System.IO;
using System.Security.Permissions;
using System.Threading;

// Request permission to create two data files.
[assembly: FileIOPermissionAttribute(SecurityAction.RequestMinimum, 
    All = @"C:\Test1@##.dat")]
[assembly: FileIOPermissionAttribute(SecurityAction.RequestMinimum, 
    All = @"C:\Test2@##.dat")]

class Test
{
    static void Main()
    {
        AutoResetEvent mainEvent = new AutoResetEvent(false);
        int workerThreads;
        int portThreads;

        ThreadPool.GetMaxThreads(out workerThreads, out portThreads);
        Console.WriteLine("\nMaximum worker threads: \t{0}" +
            "\nMaximum completion port threads: {1}",
            workerThreads, portThreads);

        ThreadPool.GetAvailableThreads(out workerThreads, 
            out portThreads);
        Console.WriteLine("\nAvailable worker threads: \t{0}" +
            "\nAvailable completion port threads: {1}\n",
            workerThreads, portThreads);

        ThreadPool.QueueUserWorkItem(new 
            WaitCallback(ThreadPoolTest.WorkItemMethod), mainEvent);
           
        // Since ThreadPool threads are background threads, 
        // wait for the work item to signal before ending Main.
        mainEvent.WaitOne(5000, false);
    }
}

class ThreadPoolTest
{
    // Maintains state information to be passed to EndWriteCallback.
    // This information allows the callback to end the asynchronous
    // write operation and signal when it is finished.
    class State
    {
        public FileStream     fStream;
        public AutoResetEvent autoEvent;

        public State(FileStream fStream, AutoResetEvent autoEvent)
        {
            this.fStream   = fStream;
            this.autoEvent = autoEvent;
        }
    }

    ThreadPoolTest() {}

    public static void WorkItemMethod(object
 mainEvent)
    {
        Console.WriteLine("\nStarting WorkItem.\n");
        AutoResetEvent autoEvent = new AutoResetEvent(false);

        // Create some data.
        const int ArraySize  = 10000;
        const int BufferSize =  1000;
        byte[] byteArray = new Byte[ArraySize];
        new Random().NextBytes(byteArray);

        // Create two files and two State objects. 
        FileStream fileWriter1 = 
            new FileStream(@"C:\Test1@##.dat", FileMode.Create,
 
            FileAccess.ReadWrite, FileShare.ReadWrite, 
            BufferSize, true);
        FileStream fileWriter2 = 
            new FileStream(@"C:\Test2@##.dat", FileMode.Create,
 
            FileAccess.ReadWrite, FileShare.ReadWrite, 
            BufferSize, true);
        State stateInfo1 = new State(fileWriter1, autoEvent);
        State stateInfo2 = new State(fileWriter2, autoEvent);

        // Asynchronously write to the files.
        fileWriter1.BeginWrite(byteArray, 0, byteArray.Length, 
            new AsyncCallback(EndWriteCallback), stateInfo1);
        fileWriter2.BeginWrite(byteArray, 0, byteArray.Length, 
            new AsyncCallback(EndWriteCallback), stateInfo2);

        // Wait for the callbacks to signal.
        autoEvent.WaitOne();
        autoEvent.WaitOne();

        fileWriter1.Close();
        fileWriter2.Close();
        Console.WriteLine("\nEnding WorkItem.\n");

        // Signal Main that the work item is finished.
        ((AutoResetEvent)mainEvent).Set();
    }

    static void EndWriteCallback(IAsyncResult
 asyncResult)
    {
        Console.WriteLine("Starting EndWriteCallback.");

        State stateInfo = (State)asyncResult.AsyncState;
        int workerThreads;
        int portThreads;
        try
        {
            ThreadPool.GetAvailableThreads(out workerThreads, 
                out portThreads);
            Console.WriteLine("\nAvailable worker threads: \t{0}" +
                "\nAvailable completion port threads: {1}\n",
                workerThreads, portThreads);

            stateInfo.fStream.EndWrite(asyncResult);

            // Sleep so the other thread has a chance to run
            // before the current thread ends.
            Thread.Sleep(1500);
        }
        finally
        {
            // Signal that the current thread is finished.
            stateInfo.autoEvent.Set();
            Console.WriteLine("Ending EndWriteCallback.");
        }
    }
}
using namespace System;
using namespace System::IO;
using namespace System::Security::Permissions;
using namespace System::Threading;

// Request permission to create two data files.

[assembly:FileIOPermissionAttribute(SecurityAction::RequestMinimum,
All="C:\\Test1#@@.dat")];
[assembly:FileIOPermissionAttribute(SecurityAction::RequestMinimum,
All="C:\\Test2#@@.dat")];
ref class ThreadPoolTest
{
private:

   // Maintains state information to be passed to EndWriteCallback.
   // This information allows the callback to end the asynchronous
   // write operation and signal when it is finished.
   ref class State
   {
   public:
      FileStream^ fStream;
      AutoResetEvent^ autoEvent;
      State( FileStream^ fStream, AutoResetEvent^ autoEvent )
      {
         this->fStream = fStream;
         this->autoEvent = autoEvent;
      }

   };


public:
   ThreadPoolTest(){}

   static void EndWriteCallback( IAsyncResult^
 asyncResult )
   {
      Console::WriteLine( "Starting EndWriteCallback." );
      State^ stateInfo = dynamic_cast<State^>(asyncResult->AsyncState);
      int workerThreads;
      int portThreads;
      try
      {
         ThreadPool::GetAvailableThreads( workerThreads, portThreads );
         Console::WriteLine( "\nAvailable worker threads: \t{0}"
         "\nAvailable completion port threads: {1}\n", workerThreads.ToString(),
 portThreads.ToString() );
         stateInfo->fStream->EndWrite( asyncResult );
         
         // Sleep so the other thread has a chance to run
         // before the current thread ends.
         Thread::Sleep( 1500 );
      }
      catch ( Exception^ e ) 
      {
      }
      finally
      {
         
         // Signal that the current thread is finished.
         stateInfo->autoEvent->Set();
         Console::WriteLine( "Ending EndWriteCallback." );
      }

   }

   static void WorkItemMethod( Object^ mainEvent
 )
   {
      Console::WriteLine( "\nStarting WorkItem.\n" );
      AutoResetEvent^ autoEvent = gcnew AutoResetEvent( false
 );
      
      // Create some data.
      const int ArraySize = 10000;
      const int BufferSize = 1000;
      array<Byte>^byteArray = gcnew array<Byte>(ArraySize);
      (gcnew Random)->NextBytes( byteArray );
      
      // Create two files and two State objects. 
      FileStream^ fileWriter1 = gcnew FileStream(  "C:\\Test1@##.dat",FileMode::Create,FileAccess::ReadWrite,FileShare::ReadWrite,BufferSize,true
 );
      FileStream^ fileWriter2 = gcnew FileStream(  "C:\\Test2@##.dat",FileMode::Create,FileAccess::ReadWrite,FileShare::ReadWrite,BufferSize,true
 );
      State^ stateInfo1 = gcnew State( fileWriter1,autoEvent );
      State^ stateInfo2 = gcnew State( fileWriter2,autoEvent );
      
      // Asynchronously write to the files.
      fileWriter1->BeginWrite( byteArray, 0, byteArray->Length, gcnew AsyncCallback(
 &ThreadPoolTest::EndWriteCallback ), stateInfo1 );
      fileWriter2->BeginWrite( byteArray, 0, byteArray->Length, gcnew AsyncCallback(
 &ThreadPoolTest::EndWriteCallback ), stateInfo2 );
      
      // Wait for each callback to finish.
      autoEvent->WaitOne();
      autoEvent->WaitOne();
      fileWriter1->Close();
      fileWriter2->Close();
      Console::WriteLine( "\nEnding WorkItem.\n" );
      
      // Signal Main that the work item is finished.
      dynamic_cast<AutoResetEvent^>(mainEvent)->Set();
   }

};

int main()
{
   AutoResetEvent^ mainEvent = gcnew AutoResetEvent( false );
   int workerThreads;
   int portThreads;
   ThreadPool::GetMaxThreads( workerThreads, portThreads );
   Console::WriteLine( "\nMaximum worker threads: \t{0}"
   "\nMaximum completion port threads: {1}", workerThreads.ToString(),
 portThreads.ToString() );
   ThreadPool::GetAvailableThreads( workerThreads, portThreads );
   Console::WriteLine( "\nAvailable worker threads: \t{0}"
   "\nAvailable completion port threads: {1}\n", workerThreads.ToString(),
 portThreads.ToString() );
   ThreadPool::QueueUserWorkItem( gcnew WaitCallback( &ThreadPoolTest::WorkItemMethod
 ), mainEvent );
   
   // Since ThreadPool threads are background threads, 
   // wait for the work item to signal before ending main().
   mainEvent->WaitOne( 5000, false );
}

import System.*;
import System.IO.*;
import System.Security.Permissions.*;
import System.Threading.*;
import System.Threading.Thread;

// Request permission to create two data files.
/** @assembly FileIOPermissionAttribute(SecurityAction.RequestMinimum,
    All = "C:\\Test1@##.dat")
 */
/** @assembly FileIOPermissionAttribute(SecurityAction.RequestMinimum,
    All = "C:\\Test2@##.dat")
 */

class Test
{
    public static void main(String[]
 args)
    {
        AutoResetEvent mainEvent = new AutoResetEvent(false);
        int workerThreads = 0;
        int portThreads = 0;

        ThreadPool.GetMaxThreads(workerThreads, portThreads);
        Console.WriteLine("\nMaximum worker threads: \t{0}" + 
            "\nMaximum completion port threads: {1}",
            String.valueOf(workerThreads), String.valueOf(portThreads));
        ThreadPool.GetAvailableThreads(workerThreads, portThreads);
        Console.WriteLine("\nAvailable worker threads: \t{0}" + 
            "\nAvailable completion port threads: {1}\n", 
            String.valueOf(workerThreads), String.valueOf(portThreads));
        ThreadPool.QueueUserWorkItem(new WaitCallback(
            ThreadPoolTest.WorkItemMethod), mainEvent);

        // Since ThreadPool threads are background threads, 
        // wait for the work item to signal before ending Main.
        mainEvent.WaitOne(5000, false);
    } //main
} //Test

class ThreadPoolTest
{
    // Maintains state information to be passed to EndWriteCallback.
    // This information allows the callback to end the asynchronous
    // write operation and signal when it is finished.
    class State
    {
        public FileStream fStream;
        public AutoResetEvent autoEvent;

        public State(FileStream fStream, AutoResetEvent autoEvent)
        {
            this.fStream = fStream;
            this.autoEvent = autoEvent;
        } //State
    } //State

    ThreadPoolTest()
    {
    } //ThreadPoolTest

    public static void WorkItemMethod(Object
 mainEvent)
    {
        Console.WriteLine("\nStarting WorkItem.\n");

        AutoResetEvent autoEvent = new AutoResetEvent(false);
      
        // Create some data.
        final int arraySize = 10000;
        final int bufferSize = 1000;
        ubyte byteArray[] = new ubyte[arraySize];

        (new Random()).NextBytes(byteArray);

        // Create two files and two State objects. 
        FileStream fileWriter1 = new FileStream("C:\\Test1@##.dat"
,
            FileMode.Create, FileAccess.ReadWrite,
            FileShare.ReadWrite, bufferSize, true);
        FileStream fileWriter2 = new FileStream("C:\\Test2@##.dat"
,
            FileMode.Create, FileAccess.ReadWrite,
            FileShare.ReadWrite, bufferSize, true);
        ThreadPoolTest test = new ThreadPoolTest();
        State stateInfo1 = test.new State(fileWriter1, autoEvent);
        State stateInfo2 = test.new State(fileWriter2, autoEvent);

        // Asynchronously write to the files.
        fileWriter1.BeginWrite(byteArray, 0, byteArray.length, 
            new AsyncCallback(EndWriteCallback), stateInfo1);
        fileWriter2.BeginWrite(byteArray, 0, byteArray.length,
            new AsyncCallback(EndWriteCallback), stateInfo2);

        // Wait for the callbacks to signal.
        autoEvent.WaitOne();
        autoEvent.WaitOne();
        fileWriter1.Close();
        fileWriter2.Close();
        Console.WriteLine("\nEnding WorkItem.\n");

        // Signal Main that the work item is finished.
        ((AutoResetEvent)(mainEvent)).Set();
    } //WorkItemMethod

    static void EndWriteCallback(IAsyncResult
 asyncResult)
    {
        Console.WriteLine("Starting EndWriteCallback.");

        State stateInfo = ((State)(asyncResult.get_AsyncState()));
        int workerThreads = 0;
        int portThreads = 0;

        try {
            ThreadPool.GetAvailableThreads(workerThreads, portThreads);
            Console.WriteLine("\nAvailable worker threads: \t{0}" +
                "\nAvailable completion port threads: {1}\n",
                String.valueOf(workerThreads), String.valueOf(portThreads));
            stateInfo.fStream.EndWrite(asyncResult);

            // Sleep so the other thread has a chance to run
            // before the current thread ends.
            Thread.Sleep(1500);
        }
        finally {
            // Signal that the current thread is finished.
            stateInfo.autoEvent.Set();
            Console.WriteLine("Ending EndWriteCallback.");
        }
    } //EndWriteCallback
} //ThreadPoolTest
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
ThreadPool クラス
ThreadPool メンバ
System.Threading 名前空間
SetMinThreads
GetMinThreads
GetMaxThreads


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

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

辞書ショートカット

すべての辞書の索引

「ThreadPool.GetAvailableThreads メソッド」の関連用語

ThreadPool.GetAvailableThreads メソッドのお隣キーワード
検索ランキング

   

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



ThreadPool.GetAvailableThreads メソッドのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

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

©2024 GRAS Group, Inc.RSS