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

Thread クラス

スレッド作成および制御し、そのスレッド優先順位設定およびステータス取得行います

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

<ComVisibleAttribute(True)> _
<ClassInterfaceAttribute(ClassInterfaceType.None)> _
Public NotInheritable Class
 Thread
    Inherits CriticalFinalizerObject
    Implements _Thread
[ComVisibleAttribute(true)] 
[ClassInterfaceAttribute(ClassInterfaceType.None)] 
public sealed class Thread : CriticalFinalizerObject,
 _Thread
[ComVisibleAttribute(true)] 
[ClassInterfaceAttribute(ClassInterfaceType::None)] 
public ref class Thread sealed : public
 CriticalFinalizerObject, _Thread
/** @attribute ComVisibleAttribute(true) */ 
/** @attribute ClassInterfaceAttribute(ClassInterfaceType.None) */ 
public final class Thread extends CriticalFinalizerObject
 implements _Thread
ComVisibleAttribute(true) 
ClassInterfaceAttribute(ClassInterfaceType.None) 
public final class Thread extends
 CriticalFinalizerObject implements _Thread
解説解説

プロセスは、そのプロセス関連付けられたプログラム コード一部実行する 1 つ上のスレッド作成できますスレッド実行するプログラム コード指定するには、ThreadStart デリゲートまたは ParameterizedThreadStart デリゲート使用しますParameterizedThreadStart デリゲート使用すると、スレッド プロシージャデータを渡すことができます

スレッドは、存在している間は常に、ThreadState で 1 つ以上定義した状態を保ちますスレッドに対しては、ThreadPriority で定義したスケジューリング優先順位要求できますが、その優先順位オペレーティング システムで有効であるかどうか保証されません。

GetHashCode を使用すると、マネージ スレッド識別子の値を取得できますスレッド有効期間の間は、この値の取得元のアプリケーション ドメインに関係なく、他のスレッドの値と衝突することはありません。

メモメモ

オペレーティング システムの ThreadId は、マネージ スレッドとの固定的な関係を一切持ちません。アンマネージ ホストは、マネージ スレッドとアンマネージ スレッドの関係を制御できるためです。たとえば、高度なホストでは、CLR Hosting API使用して同一オペレーティング システム スレッドに対して複数マネージ スレッドスケジュールしたり、1 つマネージ スレッド複数オペレーティング システム スレッド間で移動させたりすることができます

使用例使用例

単純なスレッド機能を示すコード例次に示します

Imports System
Imports System.Threading

' Simple threading scenario:  Start a Shared method running
' on a second thread.
Public Class ThreadExample
    ' The ThreadProc method is called when the thread starts.
    ' It loops ten times, writing to the console and yielding 
    ' the rest of its time slice each time, and then ends.
    Public Shared Sub ThreadProc()
        Dim i As Integer
        For i = 0 To 9
            Console.WriteLine("ThreadProc: {0}", i)
            ' Yield the rest of the time slice.
            Thread.Sleep(0)
        Next
    End Sub

    Public Shared Sub Main()
        Console.WriteLine("Main thread: Start a second thread.")
        ' The constructor for the Thread class requires a ThreadStart
 
        ' delegate.  The Visual Basic AddressOf operator creates this
        ' delegate for you.
        Dim t As New Thread(AddressOf
 ThreadProc)
        ' Start ThreadProc.  On a uniprocessor, the thread does not
 get 
        ' any processor time until the main thread yields.  Uncomment
 
        ' the Thread.Sleep that follows t.Start() to see the difference.
        t.Start()
        'Thread.Sleep(0)

        Dim i As Integer
        For i = 1 To 4
            Console.WriteLine("Main thread: Do some work.")
            Thread.Sleep(0)
        Next

        Console.WriteLine("Main thread: Call Join(), to wait until
 ThreadProc ends.")
        t.Join()
        Console.WriteLine("Main thread: ThreadProc.Join has returned.
  Press Enter to end program.")
        Console.ReadLine()
    End Sub
End Class
using System;
using System.Threading;

// Simple threading scenario:  Start a static method running
// on a second thread.
public class ThreadExample {
    // The ThreadProc method is called when the thread starts.
    // It loops ten times, writing to the console and yielding 
    // the rest of its time slice each time, and then ends.
    public static void ThreadProc()
 {
        for (int i = 0; i < 10; i++) {
            Console.WriteLine("ThreadProc: {0}", i);
            // Yield the rest of the time slice.
            Thread.Sleep(0);
        }
    }

    public static void Main()
 {
        Console.WriteLine("Main thread: Start a second thread.");
        // The constructor for the Thread class requires a ThreadStart
 
        // delegate that represents the method to be executed on the
 
        // thread.  C# simplifies the creation of this delegate.
        Thread t = new Thread(new ThreadStart(ThreadProc));
        // Start ThreadProc.  On a uniprocessor, the thread does not
 get 
        // any processor time until the main thread yields.  Uncomment
 
        // the Thread.Sleep that follows t.Start() to see the difference.
        t.Start();
        //Thread.Sleep(0);

        for (int i = 0; i < 4; i++) {
            Console.WriteLine("Main thread: Do some work.");
            Thread.Sleep(0);
        }

        Console.WriteLine("Main thread: Call Join(), to wait until ThreadProc
 ends.");
        t.Join();
        Console.WriteLine("Main thread: ThreadProc.Join has returned.  Press
 Enter to end program.");
        Console.ReadLine();
    }
}
// [C++]
// Compile using /clr option.
using namespace System;
using namespace System::Threading;

// Simple threading scenario:  Start a Shared method running
// on a second thread.
public ref class ThreadExample
{
public:

   // The ThreadProc method is called when the thread starts.
   // It loops ten times, writing to the console and yielding 
   // the rest of its time slice each time, and then ends.
   static void ThreadProc()
   {
      for ( int i = 0; i < 10; i++ )
      {
         Console::Write(  "ThreadProc: " );
         Console::WriteLine( i );
         
         // Yield the rest of the time slice.
         Thread::Sleep( 0 );

      }
   }

};

int main()
{
   Console::WriteLine( "Main thread: Start a second thread." );
   
   // Create the thread, passing a ThreadStart delegate that
   // represents the ThreadExample::ThreadProc method.  For a 
   // delegate representing a static method, no object is
   // required.
   Thread^ oThread = gcnew Thread( gcnew ThreadStart( &ThreadExample::ThreadProc
 ) );
   
   // Start the thread.  On a uniprocessor, the thread does not get
 
   // any processor time until the main thread yields.  Uncomment
   // the Thread.Sleep that follows t.Start() to see the difference.
   oThread->Start();
   
   //Thread::Sleep(0);
   for ( int i = 0; i < 4; i++ )
   {
      Console::WriteLine(  "Main thread: Do some work." );
      Thread::Sleep( 0 );

   }
   Console::WriteLine(  "Main thread: Call Join(), to wait until ThreadProc
 ends." );
   oThread->Join();
   Console::WriteLine(  "Main thread: ThreadProc.Join has returned.  Press Enter
 to end program." );
   Console::ReadLine();
   return 0;
}

import System.*;
import System.Threading.*;

// Simple threading scenario:  Start a static method running
// on a second thread.
public class ThreadExample
{
    // The ThreadProc method is called when the thread starts.
    // It loops ten times, writing to the console and yielding 
    // the rest of its time slice each time, and then ends.
    public static void ThreadProc()
 throws InterruptedException
    {
        for (int i = 0; i < 10; i++) {
            Console.WriteLine("ThreadProc: {0}", System.Convert.ToString(i));
            // Yield the rest of the time slice.
            Thread.sleep(0);
        }
    } //ThreadProc

    public static void main(String[]
 args) throws InterruptedException
    {
        Console.WriteLine("Main thread: Start a second thread.");

        // The constructor for the Thread class requires a ThreadStart
 
        // delegate that represents the method to be executed on the
 
        // thread.  J# simplifies the creation of this delegate.
        System.Threading.Thread t =
            new System.Threading.Thread(new
 ThreadStart(ThreadProc));

        // Start ThreadProc.  On a uniprocessor, the thread does not
 get 
        // any processor time until the main thread yields.  Uncomment
 
        // the Thread.Sleep that follows t.Start() to see the difference.
        t.Start();
        //Thread.Sleep(0);

        for (int i = 0; i < 4; i++) {
            Console.WriteLine("Main thread: Do some work.");
            Thread.sleep(0);
        }
        Console.WriteLine("Main thread: Call Join(), to wait until "
            + "ThreadProc ends.");
        t.Join();
        Console.WriteLine("Main thread: ThreadProc.Join has returned."
            + "  Press Enter to end program.");
        Console.ReadLine();
    } //main
} //ThreadExample

このコードによって、次のような出力生成されます。

 [VB, C++, C#]
 Main thread: Start a second thread.
 Main thread: Do some work.
 ThreadProc: 0
 Main thread: Do some work.
 ThreadProc: 1
 Main thread: Do some work.
 ThreadProc: 2
 Main thread: Do some work.
 ThreadProc: 3
 Main thread: Call Join(), to wait until ThreadProc ends.
 ThreadProc: 4
 ThreadProc: 5
 ThreadProc: 6
 ThreadProc: 7
 ThreadProc: 8
 ThreadProc: 9
 Main thread: ThreadProc.Join has returned.  Press Enter to end program.
継承階層継承階層
System.Object
   System.Runtime.ConstrainedExecution.CriticalFinalizerObject
    System.Threading.Thread
スレッド セーフスレッド セーフ

この型は、マルチスレッド操作に対して安全です。

プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照



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

辞書ショートカット

すべての辞書の索引

「Thread クラス」の関連用語

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

   

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



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

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

©2025 GRAS Group, Inc.RSS