Advertisement



< Prev
Next >



Joining of Thread - Join() method




The Join() method of Thread class is a very useful method in a situation when we want a thread to wait for another thread to finish its execution. The name Join() comes from a concept of making a thread wait until a specific thread joins it after finishing its execution.




A few overloaded versions of Join() method -:


Method Description
void Join()
This version of Join() method makes a calling thread wait for another thread(whose instance has called this method) to finish its execution.

bool Join(long milliseconds)
This version of Join() method makes a calling thread wait only for a specified time(in milliseconds) for another thread(whose instance has called this method) to finish its execution.

This method returns true if the thread has terminated, or false if the thread has not terminated after the amount of time specified.






Example of Join() method


The version of Join() method makes a calling thread wait for another thread(whose instance has called this method) to finish its execution.

//C# Example of Join() method of Thread class

using System;
using System.Threading;

class ThJoin
{
String threadName;

//Constructor of our class
public ThJoin(String name)
{
	threadName = name;
}


//Entry Point of the new thread.
public void Run()  
{
	 //Getting the reference to the currently executing thread.
	Thread th = Thread.CurrentThread;
		
	//Setting the name of our thread
	th.Name = threadName;

	Console.WriteLine(th.Name + " has stated its execution");

	try
	{
		for(int i=0;i<3;i++)
		{
			Thread.Sleep(1000); //This will make this thread sleep for 1000 ms.
			Console.WriteLine(i);	
		}
		Console.WriteLine(th.Name + " has completed");			
	}
	catch(ThreadInterruptedException e)
	{
		Console.WriteLine("Thread Interrupted" + e);
	}
}


public static void Main(String[] ar)
{
	//Getting the reference of the automatically created Main Thread
	Thread th1 = Thread.CurrentThread;

	//Setting the name of the automatically created Main Thread
	th1.Name = "Main Thread";

	Console.WriteLine(th1.Name + " has stated its execution");


	//Creating an object of our class
	//To use it to create our thread
	ThJoin ob = new ThJoin("My Thread");

	//Creating an object of Thread class by calling its constructor,
	//We are specifying the method that will be called to begin execution of our thread.
	Thread th2 = new Thread(ob.Run);


	//Calling the Start() method, which automatically calls the entry-point method of the invoked thread
	th2.Start();
	
	try
	{
		//The Main thread will wait for other thread to complete its execution and join it.
		th2.Join(); 
		Console.WriteLine(th1.Name + " has completed");
	}	
	catch(ThreadInterruptedException e)
	{
		Console.WriteLine(e);
	}
}
}


Output is - :


Main Thread has stated its execution
My Thread has stated its execution
0
1
2
My Thread has completed
Main Thread has completed


Program Analysis


The code is inside the Main() method, hence, the Main thread(default thread) runs automatically.


Advertisement




Example of Join(int milliseconds) method


Let us see a different version of Join() method i.e. Join(int milliseconds), which makes a calling thread wait only for a specified milliseconds for another thread(whose instance has called this method) to finish its execution.

//C# Example of Join(int milliseconds) method of Thread class

using System;
using System.Threading;

class ThJoin
{
String threadName;

//Constructor of our class
public ThJoin(String name)
{
	threadName = name;
}


//Entry Point of the new thread.
public void Run()  
{
	 //Getting the reference to the currently executing thread.
	Thread th = Thread.CurrentThread;
		
	//Setting the name of our thread
	th.Name = threadName;

	Console.WriteLine(th.Name + " has stated its execution");

	try
	{
		for(int i=0;i<3;i++)
		{
			Thread.Sleep(1000); //This will make this thread sleep for 1000 ms.
			Console.WriteLine(i);	
		}
		Console.WriteLine(th.Name + " has completed");			
	}
	catch(ThreadInterruptedException e)
	{
		Console.WriteLine("Thread Interrupted" + e);
	}
}


public static void Main(String[] ar)
{
	//Getting the reference of the automatically created Main Thread
	Thread th1 = Thread.CurrentThread;

	//Setting the name of the automatically created Main Thread
	th1.Name = "Main Thread";

	Console.WriteLine(th1.Name + " has stated its execution");


	//Creating an object of our class
	//To use it to create our thread
	ThJoin ob = new ThJoin("My Thread");

	//Creating an object of Thread class by calling its constructor,
	//We are specifying the method that will be called to begin execution of our thread.
	Thread th2 = new Thread(ob.Run);


	//Calling the Start() method, which automatically calls the entry-point method of the invoked thread
	th2.Start();
	
	try
	{
		//The Main thread will wait for other thread to complete its execution only for 2000ms i.e. 2 seconds.
		th2.Join(2000); 
		Console.WriteLine(th1.Name + " has completed");
	}	
	catch(ThreadInterruptedException e)
	{
		Console.WriteLine(e);
	}
}
}


Output is - :


Main Thread has stated its execution
My Thread has stated its execution
0
Main Thread has completed
1
2
My Thread has completed


Program Analysis


The code is inside the Main() method, hence, the Main thread(default thread) runs automatically.




Please share this article -




< Prev
Next >
< C# Thread Priorities
C# ThreadStateException >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement