佳佳的博客
Menu
首页
【C#】Task中的最大线程数
Posted by
佳佳
on 2017-04-20
IT
C#
今天线上发现了点问题,猜测是创建线程失败导致的,所以写了段代码测试下最大线程数。 ```csharp using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; namespace MaxThreadCountTest { class Program { private static Object lockObject = new Object(); static void Main(string[] args) { int threadCount = 0; for (int i = 0; i < 5000; i++) { Task task = Task.Factory.StartNew(() => { lock (lockObject) { threadCount++; Console.WriteLine("当前线程ID:" + Thread.CurrentThread.ManagedThreadId + " (总线程:" + threadCount + "个)"); } Thread.Sleep(Timeout.Infinite); }); } while (Console.ReadLine().ToLower() != "exit") { Console.WriteLine("当前线程数为:" + threadCount); } } } } ``` 结果最后打印的是 > 当前线程ID:1030 (总线程:1023个) 说明**默认的最大线程数是1023**,之后的线程创建也没有失败,只是进入了等待状态,待前面的线程结束后,才会真正启动并运行。
版权声明:原创文章,未经允许不得转载。
https://www.liujiajia.me/2017/4/20/【c】task中的最大线程数
“Buy me a nongfu spring”
« 【C#】【Entityframework】DateTime.Now.Date之NotSupportedException
【Java】数组和List互转 »
昵称
*
电子邮箱
*
回复内容
*
(回复审核后才会显示)
提交
目录
AUTHOR
刘佳佳
江苏 - 苏州
软件工程师