How does MaxDegreeOfParallelism work?

I am trying to understand how MaxDegreeOfParallelism affects parallelism when calling Parallel.For. Here is 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 console output 1 through 9 instantly (within ~ 0.1 second). Then once a second a new number is added - 10, 11, 12 and so on. Meanwhile, in the Windows Task Manager, I see that the number of threads running in a process increases with one new thread per second.

With this code, why don't I see the output of values ​​from 1 to 1000 instantly?

(I understand that this code may make zero sense and probably a bad idea to deploy 1000 threads on my laptop, but I want to understand what is going on here)

: , , . , MaxDegreeOfParallelism parallelism. , , 1000 , , , . , , , 32? , Parallel.For, , 20 , .

+4
1

MaxDegreeOfParallelism , .

parallelism , .NET. .

parallelism ThreadPool SetMinThreads, . , , , . , .

+3

All Articles