This article mainly includes the following:
1. Conditions that can be converted into a parallel loop
2. Usage of parallel for loop: Parallel.For
3. The use of parallel ForEach Parallel.ForEach
4. Use of Parallel LINQ (PLINQ) AsParallel ()
5. Usage and differences between parallel interrupts and parallel stops
6. External control loop cancellation method (Break,stop)
7. Handling exceptions thrown in the loop body
8. Partitioned parallel method for Small loop bodies: Parallel.ForEach
9. Maximum degree of parallelism control, Parallel.for,parallel.foreach
10. Use the local task state in the loop body (the individual understanding is that the loop body contains several stages, but this phase requires thread-safe calls to these methods)
11. Try to define your own task scheduler for the parallel loop.
12. Precautions.
-------------------Gorgeous split-line-----------------------------------------
1. Conditions that can be converted into a parallel loop
. Net Framework 4.0,visualstudio2010,xp sp3,vista sp1,win7,win8,win2008 ...
The actions on the elements in the collection are the same and do not affect each other, but the order of execution does not affect the final result.
Examples of situations in life:
A: The bank's multiple service windows as a plurality of processor cores, different customer withdrawals operations are not affected by each other. When the total customer volume is certain, it is much more efficient to open multiple windows at the same time than to open only one window.
B: After the exam, the teacher sentenced the paper, the more teachers to take the test papers, the shorter the time to finish all the papers
C: Process/Initialize an array of objects of a high order of magnitude, independent of each other
2. Usage of parallel for loop: Parallel.For
Public int 10000000 ; Double New Double [numberofsteps]; for (int0; i < numberofsteps; i++) { = DoWork (i); }
The generic sequential execution of the for
Public int 10000000 Doublenewdouble[numberofsteps]; Parallel.For (0, Numberofsteps, (i) = = {= DoWork (i); });
Parallel for
The method prototypes in the example are: public static parallelloopresult for (int frominclusive, int toexclusive, action<int> body);
The return value is Parallelloopresult, which indicates the status of the result, as defined below
Public struct Parallelloopresult { publicboolget;} Public Long Get ; } }
Parallelloopresult
There are 12 types of overloads in Parallel.For , the simplest of which is the other types of overloads that can be used to specify configurations that run in parallel, such as parallelism, unmark, and so on.
3. The use of parallel ForEach Parallel.ForEach
Not to be continued ...