Friday, 30 December 2016

Multi Threading

 in C# .NET

Multi Threading:-Ø  A single program performing multiple actions in a symaltencily is known as Multi Threading. Traditionally we come accrues a concept multi tasking. In which more then one program can execute at a given point of time.
Ø  Multi tasking is supported by the OS, where as Multi threading is supported by the language.
Ø  DOS was a single tasking. Window, Linux was a multi tasking applications.
Ø  Servers, MS Word, Excel etc… were example for multi threading applications.
Ø  A thread is a unit of execution by default every program has one thread or unit of execution, that is main thread, which is responsible in executing the code.
Ø  In single thread a model in there were multiple methods executed the execution will be one method call after the other. In the sense after completely executing one method then only the controls go’s to the other method for execution.
Ø  So, in this case until the method which is executed is completing its execution other method has to wait even if there was a delay execution of the method.
Ø  In multi threading there will be multiple unit of execution responsible in execution of code. That is for each method we use a different thread for execution. So, the execution will be as following:
1.   Time sharing.
-      This was the first principle on which multiple thread will be executing. Where the OS results some time period for teach thread to execute and makes then to execute in a symaltencily.
2.   Maximum Utilization of Resource.
-      This principle comes into picture only when the first principle violated. That is if a thread could not execute in the time allocated to it without waiting for the thread to execute the control immediately to transfer to the other thread in execution.
Ø  How to create a thread:-to create a thread we were provided with a class thread under the system. Threading Namespace each object we create for the class will be consider as one thread. Will be creating the object of thread we need to pass the method name as a parameter to its constructor.
Ex:-*  System Threading.Thread(<Method Name>)
Thread t1 = new Thread(Method1);Thread t2 = new Thread(Method2);Thread t3 = new Thread(Method3);Ø Methods and Properties of thread class:-
1.   Start()
-      Start the execution of a thread.
2.   Abort()
-      Terminates the execution of a thread.
3.   Suspend()
-      Suspends the execution of a thread, until resume was called.
4.   Resume()
-      Resumes a suspend of a thread.
5.   Sleep(int milliseconds)
-      Suspends the execution of a thread. A static which suspend the current executing thread until the given time period was elapsed.
6.   Join()
-      Makes the main thread to wait until the thread which calls leaving the program.
7.   Priority()
-      A newmaritic property for setting the thread of a given thread.
*      Add a class ThreadDemo.cs
using System.Threading;class ThreadDemo{Thread t1, t2;public ThreadDemo(){t1 =new Thread(show1);t2 =new Thread(show2);t1.Start}public void Show1(){for(int i=1; i<100; i++);{Console.WriteLine(“Test1: +i”);if(i==100)Thread.Sleep(1000);}Console.WriteLine(“Thread 1 exiting”);}public void Show2(){for(int i=1; i<100; i++);{Console.WriteLine(“Test2: +i”);}Console.WriteLine(“Thread 2 exiting”);}static void Main(){ThreadDemo obj=new ThreadDemo();Obj.t1.Join();Obj.t2.Join();Console.WriteLine(“Main Thread Exiting”);}}  Ø  Thread Priority: -
By default when there was multiple threads in execution all the threads gets executed within equal implement are priority that is OS gives same preference to all threads to share the CPU resources. If required we can change the priority of the threads and request the OS to give additional priority to any of the thread in execution. Each can be done by setting the priority with any of the 5 values.1.    Lowest
2.    Below Normal
3.    Normal(default)
4.    Above Normal
5.    Highest
Ø  The thread with the highest priority well consumes more CPU resources then the thread with lowest priority.
*  Add a class ThreadDemo.cs and write the following code.
*  using System;
using System.Threading;class ThreadDemo2{Thread t1, t2;long count1, count2;public ThreadDemo2(){t1=new Thread(IncrementCount1);t2=new Thread(IncrementCount2);//t1.Priority = ThreadPriority.Lowest;//t2.Priority = ThreadPriority.Highest;t1.Start(); t2.Start();}public void IncrementCount1(){while(true)Count1  += 1;}public void IncrementCount2(){while(true)Count2  += 1;}static void Main(){ThreadDemo2 obj =new ThreadDemo2();Obj.t1.Abort(); Obj.t2.Abort();Console.WriteLine(“Count1 :” + obj.Count1);Console.WriteLine(“Count2 :” + obj.Count2);obj.t1.Join(); obj.t2.Join();} }Ø  When you execute the above program two threads gets created incrementing the count variables in a independent loop. So, by default as all the thread have the equal priority they increment the value with equal important. In this case we can’t judge which count variable will be higher.
Ø  Now uncomment the two line of code under the constructor and then execute to check the result. Where in this case maximum the count2 value will be higher when compare be count1. Because it was given more priority than the thread1.
Ø  Thread locking (Or) Synchronization: -
Ø  In multithreading we were using a different thread to call a different method. In such cases we never have a problem but in some cases multiple threads may be calling the same method of execution. In such case we will be getting unexpected results to check this.
Ø  Add a class ThreadDemo3.cs and write the following code.
*  using System;
using System.Threading;class ThreadDemo3{Thread t1, t2, t3;public ThreadDemo3(){t1 = new Thread(Display);t2 = new Thread(Display);t3 = new Thread(Display);t1.Start(); t2.Start(); t3.Start();}public void Display(){Cosole.Write(“[C# is”);Thread.Sleep(5000);Console.WriteLine(“object oriented]”);}static void Main(){ThreadDemo3 obj = new ThreadDemo3();obj.t1.Join();}Ø  Run the above program and check the results where we can understand the problem which comes in the picture because multiple threads accessing the same problem. To over come this above problem we can lock  the code under present the method as following:
             public void Display()             {Lock(this){Cosole.Write(“[C# is”);Thread.Sleep(5000);Console.WriteLine(“object oriented ]”);}}Ø  Know run the program and check out difference in out put. Where in this case as the code under the method is lock it will allow only one thread to enter inside. And if any other thread tries to access the method at the same time it has to wait out side until the thread which is inside come out.

No comments:

Post a Comment