Today I will be talking about a new feature of Parallel Programming that was added to .Net 4.0. This article is intended for those familiar with Threads in C#.
- Stopwatch watch = new Stopwatch();
- /*Start Timer*/
- watch.Start();
- /*Task to be performed*/
- for (int index = 0; index < 15; index++)
- {
- Console.WriteLine("Digging completed for " + index + "mts. area");
- Thread.Sleep(1000);
- }
- /*Stop Timer*/
- watch.Stop();
- Console.WriteLine("Time required single handed: " + watch.Elapsed.Seconds);
Output
- Task class is used to do the unit of work you will code for instead of the previous thread model.
- Parallel class is a static class that exposes a task-based version of some parallel-nature problems.
- Stopwatch watch = new Stopwatch();
- watch.Start();
- System.Threading.Tasks.Parallel.For(0, 15, index =>
- {
- Console.WriteLine("Digging completed for " + index + "mts. area ");
- Thread.Sleep(1000);
- }
- );
- watch.Stop();
- Console.WriteLine("Time required time for Parallel digging: " + watch.Elapsed.Seconds);
Output As you can see the difference in timing required by a worker to complete the task single-handedly and by more than 1 respectively. This gives a huge improvement in execution timing when Parallel. For is used. Another alternative is by using Parallel. ForEach that is similar to a foreach loop. Parallel.Invoke Suppose concurrently I want to do many tasks, like digging, cleaning, and so on. So this can be done using the Parallel.Invoke static method. This method will allow me to add various operations and execute them concurrently in parallel.
- private void OtherTask()
- {
- //Some task
- }
- private void Digging()
- {
- //Some task
- }
- private void CleanIt()
- {
- //Some task
- }
- public void CallAllTask()
- {
- Parallel.Invoke(
- new Action(OtherTask)
- , new Action(Digging)
- , new Action(CleanIt));
- }
The code snippet above will execute all of the list of operations in parallel. ParallelLoopState Suppose I want to stop my work after digging at a 10 mts. area. If only one worker was doing this task then it would have been easy. But since many are working in these tasks and each worker is unaware of what the other is doing, I need someone that can interact with each worker and know what is the state of each person's work. In other words before stopping the Parallel Task, I need to maintain a state of the Parallel Tasks executing. This task is handled by the ParallelLoopState class. ParallelLoopState: Enables iterations of Parallel loops to interact with other iterations. There are 2 methods Break () and Stop () to end the task. The differences between the Break and Stop methods are
- Break ensures that all earlier iterations that started in the loop are executed before exiting the loop.
- Stop makes no such guarantees; it says that this loop is done and should exit as soon as possible.
- Parallel.For(0, 15, (int index, ParallelLoopState loopState) =>
- {
- if (index == 10)
- {
- loopState.Break();
- //loopState.Stop();
- }
- Console.WriteLine("Digging for " + index + "mts. area ");
- Thread.Sleep(1000);
- });
Output
- bool[] result1 = arr.AsParallel().Select(x => CheckForEven(x)).ToArray();
- Task t2 = null;
- Task t = Task.Factory.StartNew(() =>
- {
- Console.WriteLine("Digging is in progress");
- });
- Console.WriteLine("");
- t2 = t.ContinueWith((a) => {
- Console.WriteLine("Clean the area");
- for (int index = 1; index <= 5; index++)
- {
- Console.WriteLine("Cleaning...." + index);
- }
- Console.WriteLine("Cleaning done");
- });
- Task.WaitAll(t, t2);// To ensure both tasks are completed
- Console.Read();
No comments:
Post a Comment