Computer >> 컴퓨터 >  >> 프로그램 작성 >> C#

C#에서 Parallel.ForEach를 어떻게 제한할 수 있습니까?

<시간/>

병렬 Foreach

C#의 Parallel.ForEach 루프는 여러 스레드에서 실행되며 처리는 병렬 방식으로 발생합니다. Parallel.ForEach 루프는 C#의 기본 기능이 아니며 C# 4.0 이상에서 사용할 수 있습니다. Parallel.ForEach 루프를 사용하려면 using 지시문에서 System.Threading.Tasks 네임스페이스를 가져와야 합니다.

포리치

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\n");
   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\n", 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