Advertisement
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. |
//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");
}
}
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
Advertisement
//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);
}
}
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
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement