zoukankan      html  css  js  c++  java
  • 关于Parallel.ForEach的不解,请高人指点

    刚看CLR VIA C# 关于 Parallel.ForEach讲解,依葫芦画瓢自己写了一个实例,功能实现了 ,但是不明白如何实现,看源码 看的一头雾水,求高人分析,能帮忙写个简单的内部实现过程

    废话不多说  直接上代码

    //通过for实现运算
     private static long GetTotalCount(String[] strs)
     {
         long totalCount = 0;
        for (int i = 0; i < strs.Length; i++)
         {
              //假设操作耗时
                Thread.Sleep(50);
                 totalCount += strs[i].Length;
           }
                return totalCount;
     }
     1 //通过Parallel.ForEach来实现运算
     2  private static long GetParallelTotalCount(String[] strs)
     3 {
     4       long totalCount = 0;
     5       Parallel.ForEach<string, long>(strs, () => 0, (str, loopState, index, length) =>
     6            {
     7                  //假设操作耗时
     8                   Thread.Sleep(50);
     9                    return str.Length + length;
    10             },
    11                length => Interlocked.Add(ref totalCount, length));
    12 
    13        return totalCount;
    14 }    

    调用过程

     1 private static void Main(string[] args)
     2         {
     3             string[] strs = new[] {"3243","544","5445","dfg","dfgd","hghhhg"};
     4 
     5             List<string> list = new List<string>();
     6             list.AddRange(strs);
     7             list.AddRange(strs);
     8             list.AddRange(strs);
     9             list.AddRange(strs);
    10             list.AddRange(strs);
    11             list.AddRange(strs);
    12             list.AddRange(strs);
    13             list.AddRange(strs);
    14             list.AddRange(strs);
    15             list.AddRange(strs);
    16             list.AddRange(strs);
    17             list.AddRange(strs);
    18             list.AddRange(strs);
    19             list.AddRange(strs);
    20             list.AddRange(strs);
    21             list.AddRange(strs);
    22             list.AddRange(strs);
    23             list.AddRange(strs);
    24             list.AddRange(strs);
    25             list.AddRange(strs);
    26 
    27             string[] strTemp = list.ToArray();
    28 
    29             Console.WriteLine("Parallel Start");
    30             Stopwatch watch = new Stopwatch();
    31             watch.Start();
    32             long totalLength =GetParallelTotalCount(strTemp);
    33             watch.Stop();
    34             Console.WriteLine("TotalCount:{0},Time:{1}", totalLength, watch.ElapsedMilliseconds);
    35             Console.WriteLine("Parallel End");
    36             Console.WriteLine("For Start");
    37             watch.Restart();
    38             totalLength = GetTotalCount(strTemp);
    39             watch.Stop();
    40             Console.WriteLine("TotalCount:{0},Time:{1}", totalLength, watch.ElapsedMilliseconds);
    41             Console.WriteLine("For End");
    42             Console.Read();
    43         }

    得到效果:

     求不吝赐教Parallel.ForEach怎么实现的,代码中  通过Parallel.ForEach来实现运算 中 ,在ForEach终结委托里他是对长度进行了相加,为什么在主体委托中是 return str.Length + length;而不是return length;对立面如何实现不了解

    独木提示下,对代码进行了点修改

     1 //通过Parallel.ForEach来实现运算
     2  private static long GetParallelTotalCount(String[] strs)
     3 {
     4      long totalCount = 0;
     5       Parallel.ForEach<string, long>(strs, () => 0, (str, loopState, index, length) =>
     6       {
     7             //假设操作耗时
     8                                                           Console.WriteLine("ThreadId:{0},CurrentLength;{1},LastLength:{2}", Thread.CurrentThread.ManagedThreadId, str.Length, length);
     9             Thread.Sleep(50);
    10             return str.Length + length;},
    11             length => Interlocked.Add(ref totalCount, length));
    12       return totalCount;
    13 }

    得到效果:

    得出的结论也正如我图片上提到的,一些线程得到的数据看上去是数值的累加,其他一些线程驳斥了这一点。还是不太明白,最主要技术不到家,看源码看不懂

  • 相关阅读:
    Homestead 安装其它的PHP版本
    Angular调用父Scope的函数
    检测到在集成的托管管道模式下不适用的ASP.NET设置
    IE8不能保存cookie,造成response.redirect死循环的原因
    IE浏览器“自定义安全级别”不能设置的原因
    测试sql server服务是否配置正确
    Sql Server Express 2005提示"failed to generate a user instance of sql server "
    cordova使用Gradle构建下载maven太慢,使用阿里云镜像
    使用Cordova编译Android平台程序提示:Could not reserve enough space for 2097152KB object heap
    Angular动态编译Html
  • 原文地址:https://www.cnblogs.com/sjr10/p/Parallel_ForEach.html
Copyright © 2011-2022 走看看