1. Introduction
with the advent of the multicore era, parallel development is increasingly demonstrating its power! The use of parallel programs, the full use of system resources, improve the performance of the program. In. NET 4.0, Microsoft has given us a new namespace: System.Threading.Tasks.
2. Test class
usingSystem;usingSystem.Collections.Concurrent;usingSystem.Collections.Generic;usingSystem.Diagnostics;usingSystem.Linq;usingSystem.Text;usingSystem.Threading;usingSystem.Threading.Tasks;namespaceParallel Learning {classParalleldemo {PrivateStopwatch Stopwatch =NewStopwatch (); Private voidrun1 () {Thread.Sleep ( -); Console.WriteLine ("Task 1 is cost 2 sec"); } Private voidrun2 () {Thread.Sleep ( the); Console.WriteLine ("Task 2 is cost 3 sec"); } Public voidParallelinvokemethod () {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 voidParallelformethod () {Stopwatch.start (); for(inti =0; I <10000; i++) { for(intj =0; J <60000; J + +) { intsum =0; Sum+=1; }} stopwatch.stop (); Console.WriteLine ("Normalfor Run"+ Stopwatch.elapsedmilliseconds +"Ms."); Stopwatch.reset (); Stopwatch.restart (); Parallel.For (0,10000, item = { for(inti =0; I <60000; i++) { intsum =0; Sum+=item; } }); Stopwatch.stop (); Console.WriteLine ("ParallelFor Run"+ Stopwatch.elapsedmilliseconds +"Ms."); } /// <summary> ///The Parallel.For method appears in the state of looting resources, which is mainly due to concurrent access to global variables, there will be resource contention, most of the time spent on the resource waiting. /// </summary> Public voidparallelForMethod2 () {intsum =0; varobj =New Object(); Stopwatch.start (); for(inti =0; I <10000; i++) { for(intj =0; J <60000; J + +) {sum++; }} stopwatch.stop (); Console.WriteLine ("Normalfor Run"+ Stopwatch.elapsedmilliseconds +"Ms."); Stopwatch.reset (); Stopwatch.restart (); Parallel.For (0,10000, item = { for(inti =0; I <60000; i++) { //Lock the global variable to prevent the snatch from appearing wrong Lock(obj) {sum++; } } }); Stopwatch.stop (); Console.WriteLine ("ParallelFor Run"+ Stopwatch.elapsedmilliseconds +"Ms."); } Public voidParallelforeach () {List<int> list =Newlist<int>(); List. ADD (0); List. ADD (1); List. ADD (2); List. ADD (3); Parallel.ForEach (list, item={Item++; Console.WriteLine (item. ToString ()); }); } Public voidRun1 () {Thread.Sleep ( -); Console.WriteLine ("Task 1 is cost 2 sec"); Throw NewException ("Exception in Task 1"); } Public voidRun2 () {Thread.Sleep ( the); Console.WriteLine ("Task 2 is cost 3 sec"); Throw NewException ("Exception in Task 2"); } Public voidParallelbreak () {concurrentbag<int> Bag =Newconcurrentbag<int>(); Stopwatch.start (); Parallel.For (0, +, (i, state) = = { if(bag.) Count = = -) {state. Stop ();//Stop is used here, when the number reaches 300, it stops immediately; you can see the result "Bag count is 300", and if you use break to notify parallel computation as soon as the exit loop, the result may be more than 300 or 300 return; } bag. ADD (i); }); Stopwatch.stop (); Console.WriteLine ("Bag Count is"+ bag. Count +", "+stopwatch.elapsedmilliseconds); } //Catching exceptions Public voidparallelwithexception () {Stopwatch.start (); Try{Parallel.Invoke (Run1, Run2); } Catch(aggregateexception aex) {foreach(varExinchAEX. 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."); } }}View Code
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespaceParallel Learning {classProgram {Static voidMain (string[] args) {Paralleldemo PD=NewParalleldemo (); //PD. Parallelinvokemethod (); //Console.readkey (); //Pd.parallelformethod (); //Console.readkey (); //pd.parallelformethod2 (); //Console.readkey (); //Pd.parallelforeach (); //Console.readkey (); //PD. Parallelbreak (); //Console.readkey ();PD. Parallelwithexception (); Console.readkey (); } }}View Code
Parallel programming Multi-thread parallel