From the advent of the first computer to the present, computer hardware technology has been greatly developed. Whether it's personal PC or company server. Dual-core, quad-core, and eight-core CPUs are already very common. In this way, we can allocate our program to multiple computer CPUs for computing. In the past, it was very difficult to perform low-level operations on threads in parallel. added support for parallelism in net4.0, making it easy. This time I will talk about the following. NET parallel
1. Data Parallelism
2. parallel Tasks
3. Parallel Linq
4. Task Factory
5. Notes
This article mainly tells you about parallel data nonsense. The following is the beginning.
In fact, data parallelism refers to allocating data in the original set or array to multiple CPUs or threads for the same operation.. net. threading. tasks provides support classes for data parallelism, Parallel. for, Parallel. forEach is very similar to the for and foreach we often use. You do not need to create thread queues, and you do not need to use locks in the basic loop. These. net will help you with the processing. You only need to pay attention to your own business. Next let's take a look at how Parallel. For and Parallel. ForEach are used.
• Parallel. For simple use
Copy codeThe Code is as follows:
Parallel. For (0,100, I => {
Dosameting ()
});
The above example is not the shadow of the for loop that we often use. Let's talk about Parallel. for the third parameter Action <int> type delegate, no matter whether the delegate parameter is 0 or how many returns are void, how can we get Parallel. for, the following example shows how to use the local variables of the thread to store and retrieve the status of each individual task created by the for loop by using the local data of the thread, you can avoid the overhead of synchronizing a large amount of Access to the shared state. Before all iterations of a task are completed, you compute and store values instead of writing them to the shared resources of each iteration. You can then write the final result to the shared resource at one time or pass it to another method.
• Sum a list <int>. Here we assume that the List length is listLength.
Copy codeThe Code is as follows:
Parallel. For <long> (0, listLength, () => 0, (j, loop, subsum) =>
{
Subsum + = lista [j];
Return subsum;
}, (X) => Interlocked. Add (ref sum, x ));
• In reality, we often encounter the need to cancel the loop. For example, you can search for a number in the queue. How to exit the Parallel. For loop. Is it okay to use the Break keyword like for and foreach? The answer is no. This is because the break construction is effective for the loop, while the parallel loop is actually a method, not how to cancel the loop. See the following example.
Copy codeThe Code is as follows:
Parallel. For <long> (0, listLength, () => 0, (j, loop, subsum) =>
{
If (subsum> 20000)
{
Loop. Break ();
}
Subsum + = lista [j];
Return subsum;
}, (X) => Interlocked. Add (ref sum, x ));
• The simple Parallel. ForEach loop Parallel. ForEach loop works in a way similar to Parallel. For loop which partitions the source set according to the system environment and Schedules work on multiple threads. The more processors in the system, the faster the parallel method runs. For some source sets, sequential loops may be faster, depending on the size of the source and the type of work being executed.
Copy codeThe Code is as follows:
Parallel. ForEach (lista, I =>{ dosameting ();});
I wonder if you have seen the foreach shadow in this place. In fact, the last input parameter of the ForEach method in the preceding example is the Action <int> delegate. When all loops are completed, the method calls the delegate. This is the same as Parallel.. So how do we obtain the returned value is very similar to the For above. I still take the summation of the above array as an example.
Copy codeThe Code is as follows:
Parallel. ForEach <int, long> (lista, () => 0, (j, loop, subsum) =>
{
If (subsum> 20000)
{
Loop. Break ();
}
Subsum + = lista [j];
Return subsum;
}, (X) => Interlocked. Add (ref sum, x ));
• Parallel. for and for performance test comparison we will generate a random number of 10 million here to make a performance comparison For the example. The results on the author's notebook are as follows (the results may be different on your computer)
Attaches relevant code for your reference.
Copy codeThe Code is as follows:
Int listLength = 10000000;
List <int> listTask = new List <int> ();
List <int> list = new List <int> ();
Stopwatch wattings = Stopwatch. StartNew ();
Parallel. For (0, listLength, I => {
Random r = new Random (100 );
ListTask. Add (r. Next ());
});
Console. WriteLine ("parallel time consumption:" + watch1.ElapsedMilliseconds );
Stopwatch watch2 = Stopwatch. StartNew ();
For (int I = 0; I <listLength; I ++)
{
Random r = new Random (100 );
List. Add (r. Next ());
}
Console. WriteLine ("non-parallel time consumption:" + watch2.ElapsedMilliseconds );