In C # PLINQ (Parallel LINQ)

.NET Framework 3.5 introduced Language Integrated Query (LINQ), which has a unified model, type-safe way to query any  System.Collections.IEnumerable or  System.Collections.Generic.IEnumerable <T>  data source. About using LINQ function, you can refer to: https://www.cnblogs.com/zhaotianff/p/6236062.html

Parallel LINQ (PLINQ) is implemented in parallel mode LINQ

Note: The following sample code only for demonstration, its speed may not be as fast equivalent LINQ query sequence

1, how PLINQ query

ParallelEnumerable extended class provides a method AsParallel () may be performed in parallel query LINQ

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace PLINQ_demo
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             var list = new List<int>() { 12, 21, 13, 31, 14, 41, 15, 51, 16, 61 };
14 
15             var result = list.AsParallel().Where(x => x > 30);
16         }
17     }
18 }

2, PLINQ query parameters

WithDegreeOfParallelism: Specifies the maximum number applies to PLINQ parallelization of a query processor.

WithExecutionMode: specify how PLINQ should parallelize the query (even when the default behavior is to run the query sequence).

WithMergeOptions: should provide information about how PLINQ (if possible) the results of the parallel merged back into a sequence of prompts on the use of threads.

ParallelMergeOptions enumeration values ​​are as follows:

 

AutoBuffered 2

Selected output buffer size using the system are combined. Before providing results to the user query, the results will first be accumulated to the output buffer.

Default 0

Use the default merge types, namely AutoBuffered.

FullyBuffered 3

The combined use of the entire output buffers. Before providing any query results to the user, the system will first of all cumulative results.

NotBuffered 1

Without using the merge output buffer. Once the results are calculated elements to provide these elements to the user query.

WithCancellation: Specifies PLINQ should periodically monitor the status of the request to untag canceling execution and cancellation is provided. (Cancel this operation and the thread is the same, if you do not understand cancel thread, you can visit: https://docs.microsoft.com/zh-cn/dotnet/standard/threading/cancellation-in-managed- Threads )

 

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace PLINQ_demo
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {           
13             var cts = new System.Threading.CancellationTokenSource();
14 
15             try
16             {
17                 var result2 = list.AsParallel().Where(x => x > 30)
18                 .WithDegreeOfParallelism(Environment.ProcessorCount)
19                 .WithExecutionMode(ParallelExecutionMode.ForceParallelism)
20                 .WithMergeOptions(ParallelMergeOptions.Default)
21                 .WithCancellation(cts.Token);
22 
23                 PrintResult(result2);
24             }
25             catch(OperationCanceledException)
26             {
27                 Console.WriteLine("Parallel query has been canceled " );
 28              }
 29          }
 30  
31 is  
32          static  void PrintResult (the IEnumerable < int > Collection)
 33 is          {
 34 is              the foreach ( var Item in Collection)
 35              {
 36                  Console.WriteLine (Item);
 37 [              }
 38 is          }
 39      }
 40 }

 

Guess you like

Origin www.cnblogs.com/zhaotianff/p/12658004.html