5天玩转C#并行和多线程编程 —— 次日 并行集合和PLinq

  在上一篇博客 5天玩转C#并行和多线程编程 —— 第一天 认识Parallel中,咱们学习了Parallel的用法。并行编程,本质上是多线程的编程,那么当多个线程同时处理一个任务的时候,必然会出现资源访问问题,及所谓的线程安全。就像现实中,咱们开发项目,就是一个并行的例子,把不一样的模块分给不一样的人,同时进行,才能在短的时间内作出大的项目。若是你们都只管本身写本身的代码,写完后发现合并不到一块儿,那么这种并行就没有了意义。
  并行算法的出现,随之而产生的也就有了并行集合,及线程安全集合;微软向的也算周到,没有忘记linq,也推出了linq的并行版本,plinq - Parallel Linq.
 
 1、并行集合 —— 线程安全集合
  并行计算使用的多个线程同时进行计算,因此要控制每一个线程对资源的访问,咱们先来看一下平时经常使用的List<T>集合,在并行计算下的表现,新建一个控制台应用程序,添加一个PEnumerable类(固然你也直接写到main方法里面测试,建议分开写),写以下方法:
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());
      }
   }
}

点击F5运行,获得以下结果:性能

看到结果中显示的5851,可是咱们循环的是10000次啊!怎么结果不对呢?这是由于List<T>是非线程安全集合,意思就是说全部的线程均可以修改他的值。学习

下面咱们来看下并行集合 —— 线程安全集合,在System.Collections.Concurrent命名空间中,首先来看一下ConcurrentBag<T>泛型集合,其用法和List<T>相似,先来写个方法测试一下:测试

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());
      }

同时执行两个方法,结果以下:大数据

能够看到,ConcurrentBag集合的结果是正确的。下面咱们修改代码看看ConcurrentBag里面的数据究竟是怎么存放的,修改代码以下:

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 > 10)
               break;
            n++;
            Console.WriteLine("Item[{0}] = {1}",n,i);
         }
         Console.WriteLine("ConcurrentBag's max item is {0}", list.Max());

      }

先来看一下运行结果:

能够看到,ConcurrentBag中的数据并非按照顺序排列的,顺序是乱的,随机的。咱们平时使用的Max、First、Last等linq方法都还有。其时分相似Enumerable的用法,你们能够参考微软的MSDN了解它的具体用法。

关于线程安全的集合还有不少,和咱们平时用的集合都差很少,好比相似Dictionary的ConcurrentDictionary,还有ConcurrentStack,ConcurrentQueue等。

 

 2、Parallel Linq的用法及性能

一、AsParallel

前面了解了并行的For和foreach,今天就来看一下Linq的并行版本是怎么样吧?为了测试,咱们添加一个Custom类,代码以下:

public class Custom
   {
      public string Name { get; set; }
      public int Age { get; set; }
      public string Address { get; set; }
   }

 写以下测试代码:

 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 = 21, Address = "NewYork" });
            customs.Add(new Custom() { Name = "Jime", Age = 26, Address = "China" });
            customs.Add(new Custom() { Name = "Tina", Age = 29, Address = "ShangHai" });
            customs.Add(new Custom() { Name = "Luo", Age = 30, Address = "Beijing" });
            customs.Add(new Custom() { Name = "Wang", Age = 60, Address = "Guangdong" });
            customs.Add(new Custom() { Name = "Feng", Age = 25, 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);
      }

其实也就是加了一个AsParallel()方法,下面来看下运行结果:

时间相差了一倍,不过有时候不会相差这么多,要看系统当前的资源利用率。你们能够多测试一下。

其实,AsParallel()这个方法能够应用与任何集合,包括List<T>集合,从而提升查询速度和系统性能。

 

二、GroupBy方法

在项目中,咱们常常要对数据作处理,好比分组统计,咱们知道在linq中也能够实现,今天来学习一下新的ToLookup方法,写一个测试方法,代码以下:

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 = 21, Address = "NewYork" });
            customs.Add(new Custom() { Name = "Jime", Age = 26, Address = "China" });
            customs.Add(new Custom() { Name = "Tina", Age = 29, Address = "ShangHai" });
            customs.Add(new Custom() { Name = "Luo", Age = 30, Address = "Beijing" });
            customs.Add(new Custom() { Name = "Wang", Age = 60, Address = "Guangdong" });
            customs.Add(new Custom() { Name = "Feng", Age = 25, 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);
      }

运行结果以下:

ToLookup方法是将集合转换成一个只读集合,因此在大数据量分组时性能优于List.你们能够查阅相关资料,这里因为篇幅问题,再也不细说。

相关文章
相关标签/搜索