如何在C#中限制Parallel.ForEach?
并行Foreach
C#中的Parallel.ForEach循环在多个线程上运行,并且处理以并行方式进行。Parallel.ForEach循环不是C#的基本功能,并且在C#4.0及更高版本中可用。要使用Parallel.ForEach循环,我们需要在using指令中导入System.Threading.Tasks命名空间。
Foreach
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("使用foreach循环打印列表\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循环执行时间 = {0} seconds\n", stopWatch.Elapsed.TotalSeconds); Console.WriteLine("打印列表使用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