一、簡介ide
隨着多核時代的到來,並行開發愈來愈展現出它的強大威力!使用並行程序,充分的利用系統資源,提升程序的性能。在.net 4.0中,微軟給咱們提供了一個新的命名空間:System.Threading.Tasks。性能
二、測試類學習
using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; namespace Parallel學習 { class ParallelDemo { private Stopwatch stopWatch = new Stopwatch(); private void run1() { Thread.Sleep(2000); Console.WriteLine("Task 1 is cost 2 sec"); } private void run2() { Thread.Sleep(3000); Console.WriteLine("Task 2 is cost 3 sec"); } public void ParallelInvokeMethod() { stopWatch.Start(); Parallel.Invoke(run1, run2); stopWatch.Stop(); Console.WriteLine("Parallel run " + stopWatch.ElapsedMilliseconds + " ms."); stopWatch.Restart(); run1(); run2(); stopWatch.Stop(); Console.WriteLine("Parallel run " + stopWatch.ElapsedMilliseconds + " ms."); } public void parallelForMethod() { stopWatch.Start(); for (int i = 0; i < 10000; i++) { for (int j = 0; j < 60000; j++) { int sum = 0; sum += 1; } } stopWatch.Stop(); Console.WriteLine("NormalFor run " + stopWatch.ElapsedMilliseconds + " ms."); stopWatch.Reset(); stopWatch.Restart(); Parallel.For(0, 10000, item => { for (int i = 0; i < 60000; i++) { int sum = 0; sum += item; } }); stopWatch.Stop(); Console.WriteLine("ParallelFor run " + stopWatch.ElapsedMilliseconds + " ms."); } /// <summary> /// 出現搶奪資源情況下的Parallel.For方法,這主要是因爲並行同時訪問全局變量,會出現資源爭奪,大多數時間消耗在了資源等待上面。 /// </summary> public void parallelForMethod2() { int sum = 0; var obj = new object(); stopWatch.Start(); for (int i = 0; i < 10000; i++) { for (int j = 0; j < 60000; j++) { sum++; } } stopWatch.Stop(); Console.WriteLine("NormalFor run " + stopWatch.ElapsedMilliseconds + " ms."); stopWatch.Reset(); stopWatch.Restart(); Parallel.For(0, 10000, item => { for (int i = 0; i < 60000; i++) { //對全局變量加鎖,防止搶奪出現錯誤 lock (obj) { sum++; } } }); stopWatch.Stop(); Console.WriteLine("ParallelFor run " + stopWatch.ElapsedMilliseconds + " ms."); } public void parallelForeach() { List<int> list = new List<int>(); list.Add(0); list.Add(1); list.Add(2); list.Add(3); Parallel.ForEach(list, item => { item++; Console.WriteLine(item.ToString()); }); } public void Run1() { Thread.Sleep(2000); Console.WriteLine("Task 1 is cost 2 sec"); throw new Exception("Exception in task 1"); } public void Run2() { Thread.Sleep(3000); Console.WriteLine("Task 2 is cost 3 sec"); throw new Exception("Exception in task 2"); } public void ParallelBreak() { ConcurrentBag<int> bag = new ConcurrentBag<int>(); stopWatch.Start(); Parallel.For(0, 1000, (i, state) => { if (bag.Count == 300) { state.Stop();//這裏使用的是Stop,當數量達到300個時,會馬上中止;能夠看到結果"Bag count is 300",若是用break,通知並行計算儘快的退出循環,可能結果是300多個或者300個 return; } bag.Add(i); }); stopWatch.Stop(); Console.WriteLine("Bag count is " + bag.Count + ", " + stopWatch.ElapsedMilliseconds); } //捕獲異常 public void ParallelWithException() { stopWatch.Start(); try { Parallel.Invoke(Run1, Run2); } catch (AggregateException aex) { foreach (var ex in aex.InnerExceptions) { Console.WriteLine(ex.Message); } } stopWatch.Stop(); Console.WriteLine("Parallel run " + stopWatch.ElapsedMilliseconds + " ms."); stopWatch.Reset(); stopWatch.Start(); try { Run1(); } catch(Exception ex) { Console.WriteLine(ex.Message); } stopWatch.Stop(); Console.WriteLine("Normal run " + stopWatch.ElapsedMilliseconds + " ms."); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Parallel學習 { class Program { static void Main(string[] args) { ParallelDemo pd = new ParallelDemo(); //pd.ParallelInvokeMethod(); //Console.ReadKey(); //pd.parallelForMethod(); //Console.ReadKey(); //pd.parallelForMethod2(); //Console.ReadKey(); //pd.parallelForeach(); //Console.ReadKey(); //pd.ParallelBreak(); //Console.ReadKey(); pd.ParallelWithException(); Console.ReadKey(); } } }