Thread :
A Thread is a unit of execution where every program has a thread to execute the program and we call it as "Main() Thread". So, by default every program is single threaded.
- In a single threaded application the execution of program takes place by performing the actions one after the other that is it will call the methods one by one. The Drawback in this approach is if a method is taking more time to execute then the expected time the other methods which has to execute next has to wait.
For example:
Main() Thread
- Method1()
- Method2()
- Method3()
Multi-Threading:
- A single application performing multiple actions at point of time simultaneously is know Multi- Threading.
- we can overcome the above problem by using multi-threading where in this approach we have separate thread for each method to execute. When we use multi- thread in an application the execution of the programs will take place by adopting two principles.
1. Time sharing: where in this case the operating system allocates some time period for each thread to execute & transfer the controls to other threads once the given period elapses by giving equal importance for all methods to run.
2. Maximum utilization of Resource : This comes into picture when the time sharing violates i.e.., if the thread could not execute the some reason on its given time period without waiting at that method ideally that control gets transferred to the other thread in execution.
T1 - > method1()
T2 - > method2()
T3 -> method3()
The main goal of multi-threading is maximum utilization of CPU resources.
How to Create Thread ??
If we want to create thread we need to create object of class 'thread' which is present under "System.Threading" namespace by parsing the method we want to call by thread as a parameter to the constructor of the class.
System.Threading.Thread(<method1()>);
Thread t1()=new Thread(method1()) ;
Thread t2()=new Thread(method2()) ;
Thread t3()=new Thread(method3()) ;
Members of Thread class :
1. Start() :
A Method which should call on the thread object we have created to start the execution of thread.
2. Abort() :
If this method is called on a thread object, it will terminate the execution of the thread in the middle of this execution.
3. Suspend()
If will pause the execution of a thread until resume() method is called.
4. Resume()
It resumes the execution of suspend thread.
5. Sleep(int milliseconds)
It is a static method which will suspect the execution of current executing thread until the give time period elapses.
6. Join()
When this method is called on a thread object it will make the main() thread to wait without existing the program until the thread o which Join() is called.
7. Priority()
It is enumerated property using which can set the priorities between the threads in sharing the CPU resources
For Example:
using System;
using System.Threading;
class ThreadDemo
{
Thread t1,t2,t3;
public void ThreadDemo()
{
t1=new Thread( Method1);
t2=new Thread( Method2);t3=new Thread( Method3);
t1.Start(); t2.Start(); t3.Start();
}
public void Method1()
{
for(int i=1;i<=100;i++)
{
Console.WriteLine("Method1:"+i);
}
Console.WriteLine("Method1 is Exiting..");
}
public void Method2()
{
for(int i=1;i<=100;i++)
{
Console.WriteLine("Method2:"+i);
if(i==50)
{
Thread.Sleep(1000);
}
}
Console.WriteLine("Method2 is exiting");
}
public void Method3()
{
for(int i=1;i<=100;i++)
{
Console.WriteLine("Method3:"+i);
}
Console.WriteLine("Method2 is exiting");
}
public static Main()
{
ThreadDemo obj =new ThreadDemo();
obj.t1.Join();
obj.t2.Join();
obj.t3.Join();
Console.WriteLine("Main Thread is exiting");
}
}
Summary:
What do you think? Do you like this Article? Want to know more interesting concepts in .Net, then Subscribe and Follow to this blog. Have a Good Day.
No comments:
Post a Comment