Monday, 8 July 2013

Difference between Multi Threading and Parallel Programming

In this article we can see the difference between Multi threading and parallel programming

Why Dotnet Framework introduces Parallel Programming Even though Multi threading already exists in Framework

There is a major big difference is exists between the two concepts , but both are doing the multi task in concurrent way. Parallel programming is acts as higher layer of Threading.

Multi Threading

First Let we see about Multi Threading, Concepts,


From the above program we can invoke a 10 threads concurrently While execute the  multi threading ,I monitor the CPU Performance. My computer have 4 core processcor

CPU LOAD


Threads are executed in single core more , so load given to the core is not split based on balance. It is allocated based on the sequence , threads are allocated more to perform in single core , after the core is heavy load. remaining threads are then in remaining core.

Allocation of thread execution in core has to be handled manually by developers and it is more complex to find which core is heavy loaded , and  which core we have to execute the thread.

OutPut :

From the ouput you can see the time taken to finish each thread , along with thread id.



parallel Programming

Now Let we see parallel Programming and there advancements.
class Program
    {
        public static void Main(string[] args)
        {
            Action Measure= (body)=>
            {
                DateTime start=DateTime.Now;
                body();
                Console.WriteLine("{0} {1}",DateTime.Now-start,Thread.CurrentThread.ManagedThreadId);
            };
           
            Action Cal=()=>{for(int i=0;i<500000;i++);};
            ThreadPool.SetMinThreads(10,10);
           
            Parallel.For(0,10,_=>{Measure(Cal);});
                                   
            Console.WriteLine("Press any key.....");
            Console.Read();
        }
       
    }


From the above code , we are creating the threads in parallel. 10 threads are running at the same time in all the core , CPU load is balanced by allocating the threads equally in all core. So Allocating the Execution of threads to the Core is handled internally by parallel programming . Parallel.For(); is one of the code which is used to execute the method in parallel.

CPU LOAD 


From the above screenshot , we can find the parallel program or task library will allocate the threads in balanced manner to the core processors of system. Each Core of computer is equally loaded.

OUTPUT: 


From the Given output we can find that parallel programming is very fast then multi threading.

Let we see another program in parallel which uses task factory class ,Allocation of thread execution in core is managed by parallel programming internally.

  public static void Main(string[] args)
        {
            Action Measure= (body)=>
            {
                DateTime start=DateTime.Now;
                body();
                Console.WriteLine("{0} {1}",DateTime.Now-start,Thread.CurrentThread.ManagedThreadId);
            };
           
            Action Cal=()=>{for(int i=0;i<500000;i++);};
            ThreadPool.SetMinThreads(10,10);
           
            Measure(()=>
                   {                                           
                        Measure(()=>
                                {
                                    var tasks = new []{
                                                        Task.Factory.StartNew(()=>Measure(Cal)),
                                                        Task.Factory.StartNew(()=>Measure(Cal))
                                                        };
                                    Task.WaitAll(tasks);
                                });
                    });
           
                                   
            Console.WriteLine("Press any key.....");
            Console.Read();
        }
       
    }


 From this article , we can understand about the basic info of  parallel programming or task library and Multi threading. Difference between Parallel programming and muilt threading is also explained clearly