In C#, Thread class provides the Join() method which allows one thread to wait until another thread completes its execution. If t is a Thread object whose thread is currently executing, then t.Join() causes the current thread to pause its execution until thread it joins completes its execution.
If there are multiple threads calling the Join() methods that mean overloading on join allows the programmer to specify a waiting period. However, as with sleep, Join is dependent on the OS for timing, so you should not assume that join will wait exactly as long as you specify.
There are three methods in the overload list of
Thread.Join() method as follows:
- Join() Method: This method blocks the calling thread until the thread represented by this instance terminates while continuing to perform standard COM and SendMessage pumping.
public void Join ();
- Join(Int32) Method: This method blocks the calling thread until the thread represented by this instance terminates or the specified time elapses while continuing to perform standard COM and SendMessage pumping.
public bool Join (int millisecondsTimeout);
- Join(TimeSpan) Method: This method blocks the calling thread until the thread represented by this instance terminates or the specified time elapses while continuing to perform standard COM and SendMessage pumping.
public bool Join (TimeSpan timeout);
0 1 2 3 2nd thread is Working..
Explanation: In the above example, we have a class named as ExThread and this class contains a non-static method, i.e.
mythread1(). So we create an instance, i.e. obj of ExThread class and refer it in the constructor of ThreadStart class. Using
Thread thr1 = new Thread(new ThreadStart(obj.mythread)); statement we create a thread named as thr1 and initialize the work of this thread, similarly for thread thr2. By using
thr1.Join(); statement, we will send the calling of thr2 into waiting until the work of thr1 thread is completed. After that thr2 thread executes.
New thread is terminated
- Types of Threads in C#
- How to create Threads in C#
- Range Structure in C# 8.0
- Basics Operations of File and Directory in C#
- Check if the given ranges are equal or not in C#
- How to Create a Range to a Specified End in C#?
- How to Create a Range From a Specified Start in C#?
- Getting the Hash Code of the Specified Range in C#
- Range Constructor in C#
- Finding the Start Index of the Specified Range in C#
- Finding the End Index of the Specified Range in C#
- Finding all the Elements of a Range from Start to End in C#
- 5 Best Programming Languages For Newbies
- Index Struct in C# 8.0
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.