In C# the thread join class under system threading namespace consists of many methods to work with threads. One such method is Thread.Join(). This method is used to make all the calling threads wait until the current thread terminates or completes its task. Join() method in the thread can be used to achieve synchronization and it can also be used to ensure that a particular thread has been terminated.
C# provides three overloads of Join() method, which are as follows:
Syntax
The syntax of the Join() method for all its three overloads is as follows:
public void Join();
The above Join() method does not take any arguments and blocks the calling thread until the thread represented by the current instance completes its execution or terminates while performing standard COM and SendMessage pumping.
public bool Join(int millisecondsTimeout);
The above Join() method takes an integer as argument and blocks the calling thread until the thread represented by the current instance completes its execution or terminates or the time (milliseconds) specified using the integer argument ‘millisecondsTimeout’ elapses while performing standard COM and SendMessage pumping.
public bool Join(TimeSpan timeout);
This Join() method takes an argument of type TimeSpan and blocks the calling thread until the thread represented by the current instance completes its execution or terminates or the time specified using the argument ‘timeout’ elapses while performing standard COM and SendMessage pumping.
To work with threads in C#, we need to first import the System. Threading namespace in our code so that we can have access to the classes present inside Threading namespace.
The Thread class under Threading namespace contains Join() method which is used to make multiple threads work in a synchronized way by blocking the calling thread until the thread which has already called Join() method completes its task and terminates. The calling thread will be blocked for an indefinite period of time if the thread which has called Join() method does not terminate. Thus, the join() method helps us in ensuring that a particular thread has been terminated.
Join() method works on a thread which is in the alive state, we can check this by using Thread.IsAlive property. If we are calling Join() method before the thread has started then the Join() method will return immediately. In the same way, if you are calling Join() method when the thread has already terminated then also the Join() method will return immediately. Thus, the Join() method returns immediately if the thread is not running.
We should not call the Join() method of the thread object which indicates the current thread. This will make our app unresponsive because the current thread will wait upon itself indefinitely.
Please find below a table showing important details regarding three overloads of 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.
The above is the detailed content of C# Thread Join. For more information, please follow other related articles on the PHP Chinese website!