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.
MaxDegreeOfParallelism refers to the maximum number of worker tasks that will be scheduled at any one time by a parallel loop.
The degree of parallelism is automatically managed by the implementation of the Parallel class, the default task scheduler, and the .NET thread pool. The throughput is optimized under a wide range of conditions.
For very large degree of parallelism, you may also want to use the ThreadPool class’s SetMinThreads method so that these threads are created without delay. If you don't do this then the thread pool’s thread injection algorithm may limit how quickly threads can be added to the pool of worker threads that is used by the parallel loop. It may take more time than you want to create the required number of threads.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With