Joining two or more threads ensure that the execution of a thread is extended until the execution of other currently running threads is complete. To understand the concept of joining thread , let's create an application with the name ThreadJoin and add a project in it with the name NoJoin that help to execute the thread without joining them.
showing the code of NoJoin application :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace JOINING_THREADS.CS
{
class Program
{
static void Main(string[] args)
{
Thread firstthread = new Thread(firstfunction);
Thread secondthread = new Thread(secondfunction);
Thread thirdthread = new Thread(thirdfunction);
Console.WriteLine("threads starts...");
firstthread.Start();
secondthread.Start();
thirdthread.Start();
Console.WriteLine("the main() is ending...");
}
public static void firstfunction()
{
for (int num = 1; num <= 5; num++)
{
Console.WriteLine("first thread displays : \t"+num);
Thread.Sleep(2000);
}
}
public static void secondfunction()
{
for (int num = 1; num <= 5; num++)
{
Console.WriteLine("second thread displays : \t"+num);
Thread.Sleep(5000);
}
}
public static void thirdfunction()
{
for (int num = 1; num <= 5; num++)
{
Console.WriteLine("third thread displys : \t\t"+num);
Thread.Sleep(7000);
} Console.ReadKey();
}
}
}
showing the code of NoJoin application :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace JOINING_THREADS.CS
{
class Program
{
static void Main(string[] args)
{
Thread firstthread = new Thread(firstfunction);
Thread secondthread = new Thread(secondfunction);
Thread thirdthread = new Thread(thirdfunction);
Console.WriteLine("threads starts...");
firstthread.Start();
secondthread.Start();
thirdthread.Start();
Console.WriteLine("the main() is ending...");
}
public static void firstfunction()
{
for (int num = 1; num <= 5; num++)
{
Console.WriteLine("first thread displays : \t"+num);
Thread.Sleep(2000);
}
}
public static void secondfunction()
{
for (int num = 1; num <= 5; num++)
{
Console.WriteLine("second thread displays : \t"+num);
Thread.Sleep(5000);
}
}
public static void thirdfunction()
{
for (int num = 1; num <= 5; num++)
{
Console.WriteLine("third thread displys : \t\t"+num);
Thread.Sleep(7000);
} Console.ReadKey();
}
}
}
output :
In this figure , you can see that the Main() thread terminates rapidly after starting the firstthread thread and the secondthread thread. However , if you want the main() thread to stay alive until the firstthread thread is alive , then apply the join() method to the firstthread thread.
To understand this more clearly , let's implement the join() method in the ThreadJoin application of the ThreadJoin project.
showing the code of ThreadJoin application :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace JOININ_A_THREAD.CS
{
class Program
{
static void Main(string[] args)
{
Thread firstthread = new Thread(firstfunction);
Thread secondthread = new Thread(secondfunction);
Thread thirdthread = new Thread(thirdfunction);
Console.WriteLine("starts thread..");
firstthread.Start();
secondthread.Start();
thirdthread.Start();
firstthread.Join();
Console.WriteLine("\n the main() is ending..");
}
public static void firstfunction()
{
for (int num = 1; num <= 5; num++)
{
Console.WriteLine("first thread displays \t" + num);
Thread.Sleep(2000);
}
}
public static void secondfunction()
{
for (int num = 1; num <= 5; num++)
{
Console.WriteLine("second thread displays \t" + num);
Thread.Sleep(4000);
}
}
public static void thirdfunction()
{
for (int num = 1; num <= 5; num++)
{
Console.WriteLine("thread displays \t" + num);
Thread.Sleep(6000);
} Console.ReadKey();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace JOININ_A_THREAD.CS
{
class Program
{
static void Main(string[] args)
{
Thread firstthread = new Thread(firstfunction);
Thread secondthread = new Thread(secondfunction);
Thread thirdthread = new Thread(thirdfunction);
Console.WriteLine("starts thread..");
firstthread.Start();
secondthread.Start();
thirdthread.Start();
firstthread.Join();
Console.WriteLine("\n the main() is ending..");
}
public static void firstfunction()
{
for (int num = 1; num <= 5; num++)
{
Console.WriteLine("first thread displays \t" + num);
Thread.Sleep(2000);
}
}
public static void secondfunction()
{
for (int num = 1; num <= 5; num++)
{
Console.WriteLine("second thread displays \t" + num);
Thread.Sleep(4000);
}
}
public static void thirdfunction()
{
for (int num = 1; num <= 5; num++)
{
Console.WriteLine("thread displays \t" + num);
Thread.Sleep(6000);
} Console.ReadKey();
}
}
}
output :