5 days of playing C # parallel and multithreaded programming--next day parallel collections and PLINQ

Source: Internet
Author: User

5 days in the previous blog C # parallel and multithreaded programming--the first day of parallel, we learned how to use parallel. Parallel programming, in essence, is multithreaded programming, so when multiple threads handle a task at the same time, there will inevitably be resource access problems, and so-called thread safety. Just like in reality, we develop projects, is a parallel example, the different modules to different people, at the same time, in order to make a large project in a short period of time.  If we all just write their own code, after writing to find that the merge is not together, then this parallel is meaningless. The emergence of parallel algorithms, along with the creation of parallel collections, and thread-safe collections, Microsoft is thoughtful, did not forget LINQ, also introduced a parallel version of LINQ, Plinq-parallel LINQ. One, parallel set--line Shuo complete combinationParallel computing uses multiple threads to compute at the same time, so to control access to resources for each thread, let's take a look at the usual list<t> collection, perform in parallel computations, create a new console application, add a penumerable class ( Of course, you also write directly to the main method inside the test, suggested to write separately, write the following method:
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Collections.Concurrent;namespacethreadpool{ Public classpenumerable { Public Static voidListwithparallel () {List<int> list =Newlist<int>(); Parallel.For (0,10000, item ={list.         ADD (item);         }); Console.WriteLine ("List ' s count is {0}", List.      Count ()); }   }}

Click F5 to run and get the following results:

See the results shown in 5851, but we are looping 10,000 times Ah! What's wrong with the result? This is because list<t> is a non-thread-safe collection, meaning that all threads can modify his value.

Let's take a look at the parallel collection-thread-safe collection, in the System.Collections.Concurrent namespace, first look at the concurrentbag<t> generic collection, which uses a similar list<t>, Let's start by writing a test:

 Public Static void Concurrentbagwithpallel ()      {         Concurrentbag<intnew concurrentbag<int>();         Parallel.For (010000, item =         {            list. ADD (item);         });         Console.WriteLine ("concurrentbag ' s count is {0}", list. Count ());      }

Execute two methods at the same time, with the following results:

As you can see, the result of the Concurrentbag collection is correct. Let's change the code to see how the data in Concurrentbag is stored, and modify the code as follows:

 Public Static voidConcurrentbagwithpallel () {concurrentbag<int> list =Newconcurrentbag<int>(); Parallel.For (0,10000, item ={list.         ADD (item);         }); Console.WriteLine ("Concurrentbag ' s count is {0}", List.         Count ()); intn =0; foreach(intIinchlist) {            if(N >Ten)                Break; N++; Console.WriteLine ("item[{0}] = {1}", N,i); } Console.WriteLine ("concurrentbag ' s max item is {0}", List.      Max ()); }

Let's look at the results of the operation:

As you can see, the data in the concurrentbag is not arranged in order, and the order is chaotic and random. We also use the LINQ methods of Max, first, last, and so on. It is similar to the usage of enumerable, you can refer to Microsoft's MSDN to understand its specific usage.

There are a lot of thread-safe collections, similar to those we normally use, such as dictionary concurrentdictionary, and Concurrentstack,concurrentqueue.

Second, the usage and performance of Parallel LINQ

1, AsParallel

As you learned about parallel for and foreach, let's take a look at the parallel versions of LINQ today. To test, we add a custom class with the following code:

 Public class Custom   {      publicstringgetset;}        Public int Get Set ; }        Public string Get Set ; }   }

Write the following test code:

  Public Static voidtestplinq () {Stopwatch SW=NewStopwatch (); List<Custom> Customs =NewList<custom>();  for(inti =0; I <2000000; i++) {customs. ADD (NewCustom () {Name ="Jack", age = +, Address ="NewYork" }); Customs. ADD (NewCustom () {Name ="Jime", age = -, Address =" China" }); Customs. ADD (NewCustom () {Name ="Tina", age = in, Address ="Shanghai" }); Customs. ADD (NewCustom () {Name ="Luo", age = -, Address ="Beijing" }); Customs. ADD (NewCustom () {Name ="Wang", age = -, Address ="Guangdong" }); Customs. ADD (NewCustom () {Name ="Feng", age = -, Address ="Yunnan" }); } SW.         Start (); varresult = Customs. Where<custom> (c = c.age > -).         ToList (); Sw.         Stop (); Console.WriteLine ("Linq time is {0}.", SW.         Elapsedmilliseconds); Sw.         Restart (); Sw.         Start (); varRESULT2 = Customs. AsParallel (). Where<custom> (c = c.age > -).         ToList (); Sw.         Stop (); Console.WriteLine ("Parallel Linq time is {0}.", SW.      Elapsedmilliseconds); }

In fact, add a AsParallel () method, below to see the results of the operation:

The time difference is one times, but sometimes does not differ so much, depends on the system current resource utilization. You can test it a bit more.

In fact, AsParallel () This method can be applied with any set, including the List<t> collection, thus improving query speed and system performance.

2. GroupBy method

In the project, we often have to do the data processing, such as group statistics, we know in LINQ can also be implemented, today to learn about the new ToLookup method, write a test method, the code is as follows:

 Public Static voidorderbytest () {Stopwatch Stopwatch=NewStopwatch (); List<Custom> Customs =NewList<custom>();  for(inti =0; I <2000000; i++) {customs. ADD (NewCustom () {Name ="Jack", age = +, Address ="NewYork" }); Customs. ADD (NewCustom () {Name ="Jime", age = -, Address =" China" }); Customs. ADD (NewCustom () {Name ="Tina", age = in, Address ="Shanghai" }); Customs. ADD (NewCustom () {Name ="Luo", age = -, Address ="Beijing" }); Customs. ADD (NewCustom () {Name ="Wang", age = -, Address ="Guangdong" }); Customs. ADD (NewCustom () {Name ="Feng", age = -, Address ="Yunnan" });         } stopwatch.restart (); vargroupbyage = Customs. GroupBy (item =item. Age).         ToList (); foreach(varIteminchgroupbyage) {Console.WriteLine ("Age={0},count = {1}", item. Key, item.         Count ());         } stopwatch.stop (); Console.WriteLine ("Linq group by Time is:"+stopwatch.elapsedmilliseconds);         Stopwatch.restart (); varlookupList = Customs. ToLookup (i =i.age); foreach(varIteminchlookupList) {Console.WriteLine ("Lookup:age={0},count = {1}", item. Key, item.         Count ());         } stopwatch.stop (); Console.WriteLine ("LookUp group by Time is:"+stopwatch.elapsedmilliseconds); }

The results of the operation are as follows:

The ToLookup method is to convert a collection to a read-only collection, so performance is better than list when large data packets are grouped. You can consult the relevant information, here because of the space problem, no longer elaborate.

5 days of playing C # parallel and multithreaded programming--next day parallel collections and PLINQ

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.