因项目响应过慢,代码优化空间不大,在暂时无法调整系统架构的情况下,只有使用.NET中的TPL解决一些模块耗时过多的问题。但在使用过程中也碰到了一些问题,现在把它写下来,用于备忘。
1. Parallel.ForEach的使用
1 static void Main(string[] args) 2 { 3 //Test(); 4 TestParllel(); 5 Console.ReadLine(); 6 } 7 8 private static void TestParllel() 9 { 10 var list = new List<int>(6000); 11 12 for (int i = 0; i < 6000; i++) 13 { 14 list.Add(i); 15 } 16 Parallel.ForEach(list, (p, state) => { Invoke(p); }); 17 } 18 19 static void Invoke(int i) 20 { 21 Console.WriteLine(Thread.CurrentThread.ManagedThreadId); 22 Thread.Sleep(30000); 23 }