.Net 4 Parallel programming


.NET Framework 4.0 introduces new set of Types like concurrent classes, Synchronization primitives and types for lazy initialization. These are highly scalable and improve performance and can be used to perform thread safe operations in any multi threaded programs including Parallel Task.
static void Main(string[] args)
        {
            UsingParallelFor();
            UsingParallelInvoke();
            Console.ReadLine();
        }
 
 
The above methods are calling the below methods .The example reads the file values through parallel programming and print. 
   private static void UsingParallelInvoke()
        {
IEnumerable<string> words = from w in File.ReadAllText("Sample.txt").Split(' ')
                                        select w;
            Parallel.Invoke(() =>
            {
                Console.WriteLine("Task 1 Started");
                GetCommonWords(words);
            },
                    () =>
                    {
                        Console.WriteLine("Task 2 Started");
                        GetLongestWord(words);
                    },
                    () =>
                    {
                        Console.WriteLine("Task 3 Started");
                        CountWordOccurances(words, "asp.net");
                    }
            );
 
            Console.WriteLine("Back from Parallel.Invoke.");
        }
        private static void CountWordOccurances(IEnumerable<string> words, string p)
        {
            Thread.Sleep(2000);
            var searchResult = from wd in words
                               where wd.ToUpper() == p.ToUpper()
                               select wd;
            Console.WriteLine("Task 3 completed -------- '{0}' occurs {1} times", p,
earchResult.Count());
        }
 
        private static void GetLongestWord(IEnumerable<string> words)
        {
            Thread.Sleep(2000);
            var longestword = (from wd in words
                              orderby wd.Length descending
                               select wd).First();
            Console.WriteLine("Task 2 completed --------- longest word is {0}",
ongestword);
       }
        private static void GetCommonWords(IEnumerable<string> words)
        {
            Thread.Sleep(2000);
            var commonwords = (from wd in words
                              group wd by wd into grp
                              orderby grp.Count() descending
                              select grp.Key).Take(10);
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("Task 1 completed--------Most common words");
            foreach (var item in commonwords)
            {
                sb.AppendLine(item);
            }
            Console.WriteLine(sb.ToString());
        }
 
        private static void UsingParallelFor()
        {
            Stopwatch sw = Stopwatch.StartNew();
            CallNonParallelMethod();
            Console.WriteLine("Non parallel method executed in {0} secs",
w.Elapsed.Seconds);
            sw = Stopwatch.StartNew();
            CallParallelMethod();
            Console.WriteLine("Parallel method executed in {0} secs",
w.Elapsed.Seconds);
        }
        private static void CallNonParallelMethod()
        {
          for (int i = 0; i < 10; i++)
            {
                Console.WriteLine("Thread id: {0}, i: {1}",
hread.CurrentThread.ManagedThreadId, i);
                Thread.SpinWait(80000000);
            }
       }
        private static void CallParallelMethod()
        {
            Parallel.For(0, 10, i =>
                {
                    Console.WriteLine("Thread id: {0}, i: {1}",
hread.CurrentThread.ManagedThreadId, i);
                    Thread.SpinWait(80000000);
                });
        }
Out put of the application
 
 

No comments:

Post a Comment