I'm trying to understand how the MaxDegreeOfParallelism actually affects the parallelism when calling Parallel.For. Here's the code I'm experimenting with:
static void Main(string[] args)
{
var parallelOptions = new ParallelOptions()
{
MaxDegreeOfParallelism = 1000,
};
Parallel.For(1, 1000, parallelOptions, i =>
{
Console.WriteLine(i);
Thread.Sleep(TimeSpan.FromHours(1));
});
}
When I run this code, I see the console output 1 to 9 instantly (within ~0.1 second). Then once every second a new number will be added - 10, 11, 12 and so on. Meanwhile, in the Windows Task Manager I see that the number of executing threads in the process increases with one new thread per second.
With this code, why don't I see the output of the values 1 to 1000 instantly?
(I realize that this code might make zero sense and that it's probably a bad idea to spin up 1000 threads on my laptop, but I want to understand what's going on here)
EDIT: This question was - in my opinion - incorrectly marked as a duplicate. I understand that MaxDegreeOfParallelism is the max degree of parallelism. Of course there would be a large amount of context-switching if I have 1000 threads running at the same time, but the linked question does not explain how it actually works. What if I just want to run a more reasonable number of threads, say 32? My computer is well capable of handling that, but with the behavior of Parallel.For described above it takes ~20 seconds to spin up that number of threads.