我对.net核心创建的线程有点困惑。我有一个非常简单的程序,通过url数组从我们的应用程序服务器获取静态contnet:
class Program
{
static void Main(string[] args)
{
//ThreadPool.SetMaxThreads(10, 10);
var urls = new string[]
{
"http://url1.....",
"http://url2.....",
.......
};
var taskCount = 100;
var count = 1000;
var httpClient = new HttpClient {Timeout = TimeSpan.FromMilliseconds(10000)};
var tasks = new Collection<Task>();
var threads = new ConcurrentBag<int>();
for (var i = 0; i < taskCount; i++)
{
tasks.Add(Task.Run(async () =>
{
var sw = new Stopwatch();
for (var j = 0; j < count; j++)
{
foreach (var url in urls)
{
sw.Start();
try
{
threads.Add(Thread.CurrentThread.ManagedThreadId);
using (var r = await httpClient.GetAsync(url))
{
threads.Add(Thread.CurrentThread.ManagedThreadId);
if (r.StatusCode != HttpStatusCode.OK)
{
sw.Stop();
Console.WriteLine($"[{url}] - status: [{r.StatusCode}], time: [{sw.ElapsedMilliseconds}]");
}
else
{
using (var content = r.Content)
{
threads.Add(Thread.CurrentThread.ManagedThreadId);
_ = await content.ReadAsByteArrayAsync();
threads.Add(Thread.CurrentThread.ManagedThreadId);
sw.Stop();
Console.WriteLine($"[{url}] - status: [{r.StatusCode}], time: [{sw.ElapsedMilliseconds}]");
}
}
}
}
catch (Exception e)
{
Console.WriteLine(e);
}
sw.Reset();
}
}
}));
}
Console.ReadKey();
File.AppendAllText(@"E:\temp\log.txt", $"t count: {threads.ToList().Distinct().ToList().Count}\r\n");
}
}
只有100个任务按网址获取内容,记录此内容并再次执行。当我使用.NET Core 2.0在OS Windows上运行此程序时,我在日志文件中看到10个线程。我知道这些是来自线程池的线程,但是当我查看Windows资源监视器中的线程数时,我看到120个线程。为什么? 如果我使用.NET Framework 4.7.1在OS Windows上运行此程序,我会在日志文件中看到17个线程,在Windows资源监视器中看到37个线程。如果我在两种情况下都使用ThreadPool.SetMaxThreads(10,10),没有任何变化,我仍然可以通过查看Windows资源监视器看到.NET Core 2.0中的120个线程和.NET Framework 4.7.1中的大约37个线程。我不懂为什么。请有人向我解释一下这120个线程是什么以及为什么.NET Core 2.0会创建它们呢?