Advertisement



< Prev
Next >



C# Creating Multiple Thread






So far in our previous tutorials of Threads, we have only created a single thread. But what if we want to create multiple threads and perform multithreading. Before we show you how to create multiple threads, we need to remember that a thread can be created by calling different types of constructors.

Constructor Description
public Thread(ThreadStart start)
This constructor creates a new instance of Thread class. To know more about it, please read Creating a thread by based on ThreadState.

public Thread(ParamaterizedThreadStart start)
This constructor creates a new instance of Thread class, where the ParamaterizedThreadStart delegate allows us to pass an Object to the thread. To know more about it, please read Creating a thread by based on ParameterizedStateState.


Let us show you a few examples of how we can create multiple threads and perform multithreading by calling different constructors of Thread class.




Creating multiple threads based on ThreadState delegate


Let us show you how to create multiple thread based on the ThreadState delegate, which allows us to create an instance of thread without allowing us to pass an argument to it.

//C# Example of creating multiple threads based on ThreadState

using System;
using System.Threading;

class ThreadEx
{
Thread thread;

//Constructor of our class
public ThreadEx(String name)
{

	//Creating our thread, based on the object of ThreadEx class
	//We are specifying the entry-point method that will be called to begin the execution of our thread.
	thread = new Thread(this.Run);

	//Setting the name of a thread by using the Name property of Thread
	thread.Name = name;

	//Calling the Start() method, which automatically calls the entry-point method of the invoked thread
	thread.Start();
}


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

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

	try
	{
		Console.WriteLine(th.Name + " is going to sleep for 3 seconds");
		
		//Calling the Sleep() method on newly created thread
		//To make it sleep and suspend its execution for 3 seconds
		Thread.Sleep(3000);
		
		Console.WriteLine(th.Name + " is awake and exits");		
	}
	catch(ThreadInterruptedException e)
	{
		Console.WriteLine("Thread Interrupted" + e);
	}
}


public static void Main(String[] ar)
{
	//Creating three objects of our ThreadEx class
	//where each object is used to construct a separate thread
	ThreadEx ob1 = new ThreadEx("My Thread1");
	ThreadEx ob2 = new ThreadEx("My Thread2");
	ThreadEx ob3 = new ThreadEx("My Thread3");
}
}


Output is -:


My Thread1 has stated its execution
My Thread2 has stated its execution
My Thread2 is going to sleep for 3 seconds
My Thread3 has stated its execution
My Thread3 is going to sleep for 3 seconds
My Thread1 is going to sleep for 3 seconds
My Thread2 is awake and exits
My Thread3 is awake and exits
My Thread1 is awake and exits


Program Analysis


We have created 3 threads based on ThreadStart delegate, where each thread in its execution sleeps for 3 seconds, before it finishes its execution


Advertisement




Creating multiple threads based on ParamaterizedThreadStart delegate


Let us show you how to create multiple thread based on the ParamaterizedThreadStart delegate, which allows us to create an instance of thread, and allowing us to pass an Object argument to it.

//C# Example of creating multiple threads based on ParamaterizedThreadStart

using System;
using System.Threading;

class ThreadEx
{
Thread thread;

//Constructor of our class
public ThreadEx(String name, int counter)
{

	//Creating a new thread, based on the object of ThreadEx class
	//Which is referred by 'this' reference
	//We are also specifying the entry-point method that will be called to begin the execution of our thread.
	thread = new Thread(this.Run);

	//Setting the name of a thread by using the Name property of Thread
	thread.Name = name;

	//Calling the Start() method, which calls the entry-point method defined with an argument
	//And passing it an argument i.e. counter of the for loop for this thread
	thread.Start(counter);
}


//Entry Point of the new thread.
public void Run(Object count)  
{
	//Getting the reference to the currently executing thread.
	Thread th = Thread.CurrentThread;

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

	//Casting the value of count(which was internally an int), from Object to int
	int c =(int)count;

	try
	{
		for(int i=0; i<c; i++)
		{
			Console.WriteLine(th.Name + " is printing " + i);
		
			//Calling the Sleep() method on newly created thread
			//To make it sleep and suspend for 1 second after printing a number
			Thread.Sleep(1000);
		}
		Console.WriteLine(th.Name + " has finished its execution ");
	}
	catch(ThreadInterruptedException e)
	{
		Console.WriteLine("Thread Interrupted" + e);
	}
}


public static void Main(String[] ar)
{
	//Creating three objects of our ThreadEx class
	//where each object is used to construct a separate thread
	ThreadEx ob1 = new ThreadEx("My Thread1", 3);
	ThreadEx ob2 = new ThreadEx("My Thread2", 4);
	ThreadEx ob3 = new ThreadEx("My Thread3", 2);
}
}


Output is -:


My Thread1 has started its execution
My Thread1 is printing 0
My Thread3 has started its execution
My Thread3 is printing 0
My Thread2 has started its execution
My Thread2 is printing 0
My Thread1 is printing 1
My Thread3 is printing 1
My Thread2 is printing 1
My Thread1 is printing 2
My Thread3 has finished its execution
My Thread2 is printing 2
My Thread1 has finished its execution
My Thread2 is printing 3
My Thread2 has finished its execution


Program Analysis






Please share this article -




< Prev
Next >
< C# Passing and argument to a Thread
C# Thread Priorities>



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