C# スレッド結合

PHPz
リリース: 2024-09-03 15:25:40
オリジナル
539 人が閲覧しました

C# では、システム スレッド名前空間のスレッド結合クラスは、スレッドを操作するための多くのメソッドで構成されます。そのようなメソッドの 1 つが Thread.Join() です。このメソッドは、現在のスレッドが終了するかタスクを完了するまで、すべての呼び出しスレッドを待機させるために使用されます。スレッド内の Join() メソッドは同期を達成するために使用でき、特定のスレッドが確実に終了したことを確認するためにも使用できます。

C# では、次のような Join() メソッドの 3 つのオーバーロードが提供されます。

  • 参加()
  • 結合(Int32)
  • 参加(タイムスパン)

構文

3 つのオーバーロードすべてに対する Join() メソッドの構文は次のとおりです。

public void Join();
ログイン後にコピー

上記の Join() メソッドは引数をとらず、現在のインスタンスによって表されるスレッドが実行を完了するか、標準の COM および SendMessage ポンピングの実行中に終了するまで、呼び出しスレッドをブロックします。

public bool Join(int millisecondsTimeout);
ログイン後にコピー

上記の Join() メソッドは引数として整数を受け取り、現在のインスタンスによって表されるスレッドが実行を完了するか終了するか、標準の実行中に整数引数 'millisecondsTimeout' を使用して指定された時間 (ミリ秒) が経過するまで、呼び出しスレッドをブロックします。 COM と SendMessage のポンピング。

public bool Join(TimeSpan timeout);
ログイン後にコピー

この Join() メソッドは、TimeSpan 型の引数を受け取り、現在のインスタンスによって表されるスレッドが実行を完了するか終了するか、標準の COM および SendMessage ポンピングの実行中に引数 'timeout' を使用して指定された時間が経過するまで、呼び出しスレッドをブロックします。 .

C# では Thread Join() メソッドはどのように機能しますか?

C# でスレッドを操作するには、まずシステムをインポートする必要があります。コード内の Threading 名前空間により、Threading 名前空間内に存在するクラスにアクセスできるようになります。

Threading 名前空間の Thread クラスには、Join() メソッドが含まれています。このメソッドは、既に Join() メソッドを呼び出したスレッドがタスクを完了して終了するまで呼び出しスレッドをブロックすることで、複数のスレッドを同期して動作させるために使用されます。 Join() メソッドを呼び出したスレッドが終了しない場合、呼び出しスレッドは無期限にブロックされます。したがって、 join() メソッドは、特定のスレッドが確実に終了されたことを確認するのに役立ちます。

Join() メソッドはアライブ状態にあるスレッド上で動作します。Thread.IsAlive プロパティを使用してこれを確認できます。スレッドが開始される前に Join() メソッドを呼び出している場合、Join() メソッドはすぐに戻ります。同様に、スレッドがすでに終了しているときに Join() メソッドを呼び出している場合も、Join() メソッドはすぐに戻ります。したがって、スレッドが実行中でない場合、Join() メソッドはすぐに戻ります。

現在のスレッドを示すスレッド オブジェクトの Join() メソッドを呼び出すべきではありません。これにより、現在のスレッドが無期限に待機するため、アプリが応答しなくなります。

Join() メソッドの 3 つのオーバーロードに関する重要な詳細を示す表を以下に示します。

Method Parameters Returns
public void Join() It does not take any arguments. Returns void.
public bool Join(int millisecondsTimeout) An integer representing the number of milliseconds required to wait for the thread to terminate. Returns Boolean value; returns true if the thread has terminated and returns false if the time specified by the parameter has elapsed and the thread has not terminated.
public bool Join(TimeSpan timeout) A TimeSpan which indicates the amount of time required to wait for the thread to terminate. Returns Boolean value; returns true if the thread has terminated and returns false if the time specified by the parameter has elapsed and the thread has not terminated.
メソッド パラメータ 返品 public void Join() 引数は必要ありません。 void を返します。 public bool Join(int ミリ秒タイムアウト) スレッドの終了を待つのに必要なミリ秒数を表す整数。 ブール値を返します。スレッドが終了した場合は true を返し、パラメータで指定された時間が経過してもスレッドが終了していない場合は false を返します。 public bool Join(TimeSpan タイムアウト) スレッドが終了するまでの待機時間を示す TimeSpan。 ブール値を返します。スレッドが終了した場合は true を返し、パラメータで指定された時間が経過してもスレッドが終了していない場合は false を返します。 テーブル>

Examples of C# Thread Join

Below are the examples of c# thread join:

Example #1

Example showing the use of Join() method on first thread and other two threads in the code wait until the first thread completes its execution.

Code:

using System;
using System.Threading;
namespace ConsoleApp4
{
public class Program
{
public void Display()
{
for (int i = 1; i <= 5; i++)
{
Console.WriteLine(Thread.CurrentThread.Name+" : " + i);
//suspending thread for specified time
Thread.Sleep(200);
}
}
}
public class ThreadDemo
{
public static void Main()
{
Program obj = new Program();
Thread thread1 = new Thread(new ThreadStart(obj.Display));
thread1.Name = "Thread1";
Thread thread2 = new Thread(new ThreadStart(obj.Display));
thread2.Name = "Thread2";
Thread thread3 = new Thread(new ThreadStart(obj.Display));
thread3.Name = "Thread3";
//starting thread1
thread1.Start();
//calling Join() on thread1
thread1.Join();
//starting thread2
thread2.Start();
//starting thread3
thread3.Start();
Console.ReadLine();
}
}
}
ログイン後にコピー

Output:

C# スレッド結合

Example #2

Example calling Join() method on all threads.

Code:

using System;
using System.Threading;
namespace ConsoleApp4
{
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Main thread started");
Thread thread1 = new Thread(Method1);
Thread thread2 = new Thread(Method2);
Thread thread3 = new Thread(Method3);
thread1.Start();
thread2.Start();
thread3.Start();
thread1.Join();
thread2.Join();
thread3.Join();
Console.WriteLine("Main thread ended");
Console.ReadLine();
}
public static void Method1()
{
Console.WriteLine("Method1 - Thread1 starting execution");
Thread.Sleep(1000);
Console.WriteLine("Method1 - Thread1 execution completed");
}
public static void Method2()
{
Console.WriteLine("Method2 - Thread2 starting execution");
Thread.Sleep(2500);
Console.WriteLine("Method2 - Thread2 execution completed");
}
public static void Method3()
{
Console.WriteLine("Method3 - Thread3 starting execution");
Thread.Sleep(5000);
Console.WriteLine("Method3 - Thread3 execution completed");
}
}
}
ログイン後にコピー

Output:

C# スレッド結合

Example #3

Example of Join(int millisecondsTimeout) method.

Code:

using System;
using System.Threading;
namespace ConsoleApp4
{
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Main thread started");
Thread thread1 = new Thread(Method1);
Thread thread2 = new Thread(Method2);
Thread thread3 = new Thread(Method3);
thread1.Start();
thread2.Start();
thread3.Start();
if (thread3.Join(2000))
{
Console.WriteLine("Execution of thread3 completed in 2 seconds.");
}
else
{
Console.WriteLine("Execution of thread3 not completed in 2 seconds.");
}
Console.WriteLine("Main thread ended");
Console.ReadLine();
}
public static void Method1()
{
Console.WriteLine("Method1 - Thread1 execution started");
Thread.Sleep(1000);
Console.WriteLine("Method1 - Thread1 execution completed");
}
public static void Method2()
{
Console.WriteLine("Method2 - Thread2 execution started");
Thread.Sleep(2500);
Console.WriteLine("Method2 - Thread2 execution completed");
}
public static void Method3()
{
Console.WriteLine("Method3 - Thread3 execution started");
Thread.Sleep(5000);
Console.WriteLine("Method3 - Thread3 execution completed");
}
}
}
ログイン後にコピー

Output:

C# スレッド結合

Conclusion

Join() method in C# makes one thread wait till another thread completes its task and terminates. There are three overloads of the Join() method in C#. Join() method works on the thread which is in the alive state. It helps to achieve synchronization while working with multiple threads.

以上がC# スレッド結合の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

関連ラベル:
ソース:php
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!