Advertisement
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. |
//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);
}
}
}
Main Thread has stated its execution
My Thread has stated its execution
0
1
2
My Thread has completed
Main Thread has completed
Advertisement
//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);
}
}
}
Main Thread has stated its execution
My Thread has stated its execution
0
Main Thread has completed
1
2
My Thread has completed
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement