首頁 > 後端開發 > C#.Net教程 > 如何在 C# 中限制 Parallel.ForEach?

如何在 C# 中限制 Parallel.ForEach?

王林
發布: 2023-09-08 19:09:07
轉載
692 人瀏覽過

如何在 C# 中限制 Parallel.ForEach?

Parallel Foreach

C# 中的 Parallel.ForEach 迴圈在多個執行緒上運行,並且處理以並行方式進行。 Parallel.ForEach 循環不是 C# 的基本功能,從 C# 4.0 及更高版本開始提供。要使用 Parallel.ForEach 循環,我們需要在 using 指令中匯入 System.Threading.Tasks 命名空間。

Foreach h2>

C# 中的 Foreach 迴圈在單一執行緒上運行,並且處理按順序逐一進行。 Foreach 迴圈是 C# 的基本功能,從 C# 1.0 開始提供。在大多數情況下,它的執行速度比 Parallel.Foreach 慢。

範例1

static void Main(string[] args){
   List<string> alphabets = new List<string>();
   alphabets.Add("A");
   alphabets.Add("B");
   alphabets.Add("C");
   alphabets.Add("D");
   alphabets.Add("E");
   alphabets.Add("F");
   alphabets.Add("G");
   alphabets.Add("H");
   alphabets.Add("I");
   alphabets.Add("J");
   alphabets.Add("K");
   alphabets.Add("L");
   alphabets.Add("M");
   alphabets.Add("N");
   alphabets.Add("O");
   alphabets.Add("P");
   alphabets.Add("Q");
   alphabets.Add("R");
   alphabets.Add("S");
   alphabets.Add("T");
   alphabets.Add("U");
   alphabets.Add("V");
   alphabets.Add("W");
   alphabets.Add("X");
   alphabets.Add("Y");
   alphabets.Add("Z");
   Console.WriteLine("Printing list using foreach loop");
   var stopWatch = Stopwatch.StartNew();
   foreach (string alphabet in alphabets){
      Console.WriteLine("alphabet Name: {0}, Thread Id= {1}", alphabet,          Thread.CurrentThread.ManagedThreadId);
   }
   Console.WriteLine("foreach loop execution time = {0} seconds", stopWatch.Elapsed.TotalSeconds);
   Console.WriteLine("Printing list using Parallel.ForEach");
   stopWatch = Stopwatch.StartNew();
   Parallel.ForEach(alphabets, alphabet =>
   {
      Console.WriteLine("alphabet Name: {0}, Thread Id= {1}", alphabet,       Thread.CurrentThread.ManagedThreadId);
   }
   );
   Console.WriteLine("Parallel.ForEach() execution time = {0} seconds",    stopWatch.Elapsed.TotalSeconds);
   Console.Read();
   Console.ReadLine();
}
登入後複製

輸出

使用foreach循環列印清單

alphabet Name: A, Thread Id= 1
alphabet Name: B, Thread Id= 1
alphabet Name: C, Thread Id= 1
alphabet Name: D, Thread Id= 1
alphabet Name: E, Thread Id= 1
alphabet Name: F, Thread Id= 1
alphabet Name: G, Thread Id= 1
alphabet Name: H, Thread Id= 1
alphabet Name: I, Thread Id= 1
alphabet Name: J, Thread Id= 1
alphabet Name: K, Thread Id= 1
alphabet Name: L, Thread Id= 1
alphabet Name: M, Thread Id= 1
alphabet Name: N, Thread Id= 1
alphabet Name: O, Thread Id= 1
alphabet Name: P, Thread Id= 1
alphabet Name: Q, Thread Id= 1
alphabet Name: R, Thread Id= 1
alphabet Name: S, Thread Id= 1
alphabet Name: T, Thread Id= 1
alphabet Name: U, Thread Id= 1
alphabet Name: V, Thread Id= 1
alphabet Name: W, Thread Id= 1
alphabet Name: X, Thread Id= 1
alphabet Name: Y, Thread Id= 1
alphabet Name: Z, Thread Id= 1
foreach loop execution time = 0.0223421 seconds
登入後複製

使用Parallel.ForEach 列印清單

alphabet Name: A, Thread Id= 1
alphabet Name: G, Thread Id= 4
alphabet Name: H, Thread Id= 4
alphabet Name: I, Thread Id= 4
alphabet Name: J, Thread Id= 4
alphabet Name: K, Thread Id= 4
alphabet Name: L, Thread Id= 4
alphabet Name: N, Thread Id= 4
alphabet Name: O, Thread Id= 4
alphabet Name: P, Thread Id= 4
alphabet Name: Q, Thread Id= 4
alphabet Name: Y, Thread Id= 6
alphabet Name: Z, Thread Id= 6
alphabet Name: D, Thread Id= 6
alphabet Name: E, Thread Id= 6
alphabet Name: F, Thread Id= 6
alphabet Name: T, Thread Id= 6
alphabet Name: U, Thread Id= 6
alphabet Name: V, Thread Id= 6
alphabet Name: R, Thread Id= 4
alphabet Name: M, Thread Id= 5
alphabet Name: S, Thread Id= 7
alphabet Name: B, Thread Id= 1
alphabet Name: C, Thread Id= 1
alphabet Name: W, Thread Id= 6
alphabet Name: X, Thread Id= 6
Parallel.ForEach() execution time = 0.0559777 seconds
登入後複製

範例2

限制parallel.foreach中的平行性

static class Program{
   static void Main(string[] args){
      List<string> alphabets = new List<string>();
      alphabets.Add("A");
      alphabets.Add("B");
      alphabets.Add("C");
      alphabets.Add("D");
      alphabets.Add("E");
      alphabets.Add("F");
      alphabets.Add("G");
      alphabets.Add("H");
      alphabets.Add("I");
      alphabets.Add("J");
      alphabets.Add("K");
      alphabets.Add("L");
      alphabets.Add("M");
      alphabets.Add("N");
      alphabets.Add("O");
      alphabets.Add("P");
      alphabets.Add("Q");
      alphabets.Add("R");
      alphabets.Add("S");
      alphabets.Add("T");
      alphabets.Add("U");
      alphabets.Add("V");
      alphabets.Add("W");
      alphabets.Add("X");
      alphabets.Add("Y");
      alphabets.Add("Z");
      Parallel.ForEach(
      alphabets,
      new ParallelOptions { MaxDegreeOfParallelism = 2 },
      alphabet => { Console.WriteLine("alphabet Name: {0}, Thread Id= {1}", alphabet,       Thread.CurrentThread.ManagedThreadId); }
   );
   }
}
登入後複製

輸出

alphabet Name: N, Thread Id= 4
alphabet Name: O, Thread Id= 4
alphabet Name: P, Thread Id= 4
alphabet Name: A, Thread Id= 1
alphabet Name: B, Thread Id= 1
alphabet Name: C, Thread Id= 1
alphabet Name: Q, Thread Id= 4
alphabet Name: R, Thread Id= 4
alphabet Name: S, Thread Id= 4
alphabet Name: T, Thread Id= 4
alphabet Name: U, Thread Id= 4
alphabet Name: V, Thread Id= 4
alphabet Name: W, Thread Id= 4
alphabet Name: X, Thread Id= 4
alphabet Name: Y, Thread Id= 4
alphabet Name: Z, Thread Id= 4
alphabet Name: H, Thread Id= 4
alphabet Name: I, Thread Id= 4
alphabet Name: J, Thread Id= 4
alphabet Name: K, Thread Id= 4
alphabet Name: D, Thread Id= 1
alphabet Name: L, Thread Id= 4
alphabet Name: E, Thread Id= 1
alphabet Name: F, Thread Id= 1
alphabet Name: G, Thread Id= 1
alphabet Name: M, Thread Id= 4
登入後複製

以上是如何在 C# 中限制 Parallel.ForEach?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:tutorialspoint.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板