在 C# 中,系統執行緒命名空間下的執行緒連接類別包含許多處理執行緒的方法。其中一個方法是 Thread.Join()。此方法用於使所有呼叫執行緒等待,直到當前執行緒終止或完成其任務。執行緒中的Join()方法可以用來實現同步,也可以用來確保特定執行緒已經終止。
C#提供了三種Join()方法的重載,如下:
文法
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 泵送時使用參數“超時”指定的時間過去.
要在 C# 中使用線程,我們需要先匯入 System.在我們的程式碼中使用 Threading 命名空間,以便我們可以存取 Threading 命名空間中存在的類別。
Threading 命名空間下的 Thread 類別包含 Join() 方法,該方法用於透過阻塞呼叫執行緒直到已經呼叫 Join() 方法的執行緒完成其任務並終止來使多個執行緒以同步方式運作。如果呼叫Join()方法的執行緒沒有終止,則呼叫執行緒將被無限期地阻塞。因此,join() 方法可以幫助我們確保特定執行緒已終止。
Join() 方法適用於處於活動狀態的線程,我們可以使用 Thread.IsAlive 屬性來檢查這一點。如果我們在執行緒啟動之前呼叫 Join() 方法,那麼 Join() 方法將立即傳回。同樣,如果在執行緒已經終止時呼叫 Join() 方法,那麼 Join() 方法也會立即傳回。因此,如果執行緒沒有運行,Join() 方法會立即回傳。
我們不應該呼叫指示目前執行緒的執行緒物件的Join()方法。這將使我們的應用程式無回應,因為當前執行緒將無限期地等待自身。
請在下面的表格中找到有關 Join() 方法的三個重載的重要詳細資訊:
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. |
Below are the examples of c# thread join:
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:
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:
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:
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中文網其他相關文章!