use of Thread Join method in C sharp

Parent Thread execution may terminates before child execution complete.
we can use Thread.Join() method to halt the other thread, until the first thread complete its execution .

Thread.Join() when attached to any thread, it makes the other thread to execute first and halts other threads. Now on the same we will see an example where we can apply join method to thread.

If we don’t use join() method in main() thread, main thread may die before child thread.

C# Thread program example without using join method

Code:

using System;
using System.Threading;
using System.Threading.Tasks;
 class Program
    {
        
        static void Main(string[] args)
        {
            Console.WriteLine("Main Thread Started");
            //Both thread will print a same string on console.
            Thread t1 = new Thread(printer);
            Thread t2 = new Thread(printer);
            t1.Start();
            t2.Start();
            //t2.Join();

            Console.WriteLine("Main Thread Ends");

        }

        static void printer()
        {
            String arr = "finesrc  is the excellent resource for interview preparation!!!";

            

                for (int i = 0; i < arr.Length; i++)
                {
                    Console.Write(arr[i]);
                }
            
        }
    }

Output

Main Thread Started
finesrc iMain Thread Ends
s the

Note that in above output main thread is already dead without waiting for child thread to finish.

C# Thread program example using join method

Code:

using System;
using System.Threading;
using System.Threading.Tasks;
 class Program
    {
        
        static void Main(string[] args)
        {
            Console.WriteLine("Main Thread Started");
            //Both thread will print a same string on console.
            Thread t1 = new Thread(printer);
            Thread t2 = new Thread(printer);
            t1.Start();
            t2.Start();
             //Wait for thread t1,t2 to finish
            t2.Join();

            Console.WriteLine("Main Thread Ends");

        }

        static void printer()
        {
            String arr = "finesrc  is the excellent resource for interview preparation!!!";

            

                for (int i = 0; i < arr.Length; i++)
                {
                    Console.Write(arr[i]);
                }
            
        }
    }

Output

Main Thread Started
finesrc finesrc is the excellent resource for interview preparation!!! is the e
xcellent resource for interview preparation!!!Main Thread Ends

In above C# source code, main or parent thread will join multiple threads. means, main thread will wait for all threads to finish before continuing.

Notes:

1 – If a thread have multiple child threads and it wants to wait for all threads to finish then we have to apply join on all threads for example

t1.Start();
t2.Start();
t3.Start(); etc.

2- To notice the output, if main thread die before Child thread without using join, put long running task to child threads.

Related Posts