Explain Multithreading with an example?
kumar M
Every process must have at least one thread. The first thread is created with a process and is known as primary thread. This Primary Thread is entry point of application. In traditional Windows applications it is the method WinMain() and in console applications it is named main().
Main goal of creating multithreading application is performmance improvement. As an example, imagine a situation where in a user starts a long process (e.g. copying), he can?t use a single threaded application and wait for an infinite time for the operation to get completed. But if he uses multi?threading application he can set copying process in the background and interact with application without any problems.
At first, if one wants to create a multi threaded application an important point to be remembered is, a global variable, which is being accessed by different threads, can try to modify the same variable. This is a generic problem, which is solved using a mechanism called Synchronization of threads. Synchronization is nothing but the process of creating some set of rules to operate data or resources.
The C# .Net language has a powerful namespace which can be used for programming with Threads as well as Thread Synchronization in C# .Net programming. The name of the namespace is Sytem.Threading. The most important class inside this namespace for manipulating the threads is the C# .Net class Thread. It can run other thread in our application process.
The example it creates an additional C# .Net class Launcher. It has only one method, which output countdown in the console.
//Sample for C# tutorial on Multithreading using lock
public void Coundown(){
lock(this){ for(int i=4;i>=0;i--){ Console.WriteLine("{0} seconds to start",i); } Console.WriteLine("GO!!!!!"); }
lock(this){
for(int i=4;i>=0;i--){ Console.WriteLine("{0} seconds to start",i); } Console.WriteLine("GO!!!!!");
for(int i=4;i>=0;i--){
Console.WriteLine("{0} seconds to start",i);
}
Console.WriteLine("GO!!!!!");
There is a new keyword lock inside the above chunk of .Net C# tutorial code. This provides a mechanism for synchronizing the thread operation. It means at the same point of time only one thread can access to this method of created object. Unless the lock is released after completion of the code, the next routine or iteration cannot enter the block.
To understand it more clearly please have a look at the piece of main method?s code:
Launcher la = new Launcher();Thread firstThread = new Thread(new ThreadStart(la.Coundown));Thread secondThread =new Thread(new ThreadStart(la.Coundown));Thread thirdThread = new Thread(new ThreadStart(la.Coundown));firstThread.Start();secondThread.Start();thirdThread.Start();
As you see there were created three additional threads. These threads start a method of object that has Launcher type. The above program is a very simple example of using multi-threading in C#. Net. But C# .Net allows us to create more powerful applications with any level of complexity.