因项目响应过慢,代码优化空间不大,在暂时无法调整系统架构的情况下,只有使用.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         }
View Code

相关文章:

  • 2022-12-23
  • 2021-11-07
  • 2021-06-12
  • 2021-08-19
  • 2022-01-17
  • 2021-06-25
  • 2022-12-23
  • 2021-07-08
猜你喜欢
  • 2022-01-21
  • 2022-01-23
  • 2022-12-23
  • 2021-10-18
  • 2022-02-01
  • 2021-06-18
  • 2021-10-08
相关资源
相似解决方案