LINQ queries which are designed to run in Parallel are termed as PLINQ queries.
The framework of PLINQ has been optimized in such a way that it includes determining if a query can perform faster in a synchronous manner. Is analyze the Linq query at run time if its likely to benefit from parallelization, when run concurrently.
The TPL library provides the following methods to support the parallel Linq queries.
Specifies that the rest of the query should be parallelized, if possible.
Specifies that PLINQ should periodically check for the state o fthe provided CancellationToken and cancel execution if it is required.
Specifies the maximum number of processors that PLINQ should use to parallelize the query.
Enables results to be processed in parallel without first merging back to the consumer thread, as would be the case when enumerating a LINQ result using the foreach keyword.
Let see the demo
Creating first PLINQ query:
if you want to use TPL to execute your query in parallel(if possible), you will want to use the AsParallel() extension method:
Here is the complete code to process and random array and analyze the Execution time:
Let my code to choose if CPU cores can be uitilized!