Task 线程重用导致等待!
测试代码:
for (int i = 0 ; i < 20 ; i++) { var d = DateTime.Now; Task.Run(() => { Console.WriteLine($"{Thread.CurrentThread.ManagedThreadId}|{Task.CurrentId}|{(DateTime.Now-d).TotalMilliseconds}"); Thread.Sleep(10000); }); } Console.ReadKey();
输出:
6|2|10.971
7|4|9.9732
5|3|11.9689
4|5|12.9661
3|1|50.8655
9|6|11.9672
10|7|15.9574
8|8|19.9469
11|9|998.3331
12|10|1997.0452
13|11|2996.8728
14|12|3995.9631
15|13|4995.9182
16|14|5997.539
17|15|6996.5269
18|16|8005.054
19|17|8995.6457
20|18|9995.6924
5|19|10003.6819
7|20|10003.6819
因为它是基于 CLR 线程池的
所以
ThreadPool.GetMinThreads(out int a, out int b);
ThreadPool.SetMinThreads(a + 20, a + 20);
这样就行了
当然 直接 new Thread 也行