2
我只是做了一個簡單的實驗,使用下面的代碼顯示進程中的線程總數。Parallel.ForEach期間使用的線程數
Console.WriteLine("Total Number of Threads: {0}", Process.GetCurrentProcess().Threads.Count);
Parallel.ForEach(
names,
new ParallelOptions {MaxDegreeOfParallelism = Environment.ProcessorCount },
name =>
{
Console.WriteLine(name);
});
Console.Read();
Console.WriteLine("Total Number of Threads: {0}", Process.GetCurrentProcess().Threads.Count);
我在parallel.foreach之前得到了12個線程,並且在parallel.foreach之後得到了17個線程。
問題:
- 這是爲什麼在Parallel.Foreach使用的5個線程繼續循環後運行?這是否意味着如果在此之後我有其他的Parallel.Foreach,會有更多的線程繼續增加?
- 爲什麼Parallel.Foreach之前不使用12個線程?是否因爲12個線程目前正在被其他人使用?