關於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 }

得到效果:

得出的結論也正如我圖片上提到的,一些線程得到的數據看上去是數值的累加,其他一些線程駁斥了這一點。還是不太明白,最主要技術不到家,看源碼看不懂

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM