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:
Using system;using system.collections.generic;using system.linq;using system.text;using System.Threading.Tasks; Using System.collections.concurrent;namespace threadpool{public   class penumerable   {public      static void Listwithparallel ()      {         list<int> List = new list<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<int> list = new concurrentbag<int> () ;         Parallel.For (0, 10000, 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 void Concurrentbagwithpallel ()      {         concurrentbag<int> list = new concurrentbag<int> () ;         Parallel.For (0, 10000, item =         {            list. ADD (item);         });         Console.WriteLine ("Concurrentbag ' s count is {0}", list. Count ());         int n = 0;         foreach (int i in list)         {            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   {public      string Name {get; set;}      public int Age {get; set;}      public string Address {get; set;}   }

Write the following test code:

 public static void Testplinq () {Stopwatch SW = new Stopwatch ();         list<custom> customs = new List<custom> (); for (int i = 0; i < 2000000; i++) {customs.            ADD (New Custom () {Name = "Jack", age = +, Address = "NewYork"}); Customs.            ADD (New Custom () {Name = "jime", age = +, Address = "China"}); Customs.            ADD (New Custom () {Name = "Tina", age = $, Address = "Shanghai"}); Customs.            ADD (New Custom () {Name = "Luo", age = +, Address = "Beijing"}); Customs.            ADD (New Custom () {Name = "Wang", age = $, Address = "Guangdong"}); Customs.         ADD (New Custom () {Name = "Feng", age = +, Address = "Yunnan"}); } SW.         Start (); var result = Customs. Where<custom> (c = c.age > 26).         ToList (); Sw.         Stop (); Console.WriteLine ("Linq time is {0}.", SW.         Elapsedmilliseconds); Sw.         Restart (); Sw.         Start (); var result2 = customs. AsparallEl (). Where<custom> (c = c.age > 26).         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 void Orderbytest () {Stopwatch Stopwatch = new Stopwatch ();         list<custom> customs = new List<custom> (); for (int i = 0; i < 2000000; i++) {customs.            ADD (New Custom () {Name = "Jack", age = +, Address = "NewYork"}); Customs.            ADD (New Custom () {Name = "jime", age = +, Address = "China"}); Customs.            ADD (New Custom () {Name = "Tina", age = $, Address = "Shanghai"}); Customs.            ADD (New Custom () {Name = "Luo", age = +, Address = "Beijing"}); Customs.            ADD (New Custom () {Name = "Wang", age = $, Address = "Guangdong"}); Customs.         ADD (New Custom () {Name = "Feng", age = +, Address = "Yunnan"});         } stopwatch.restart (); var groupbyage = customs. GroupBy (item = Item). Age).         ToList (); foreach (var item in groupbyage) {Console.WriteLine ("Age={0},count = {1}", item. Key, item.         Count ()); } stopwatch.stop ();         Console.WriteLine ("Linq Group by Time is:" + stopwatch.elapsedmilliseconds);         Stopwatch.restart (); var lookupList = customs.         ToLookup (i = i.age); foreach (var item in lookupList) {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.