Parallel.ForEach does not use all available thread pool threads

Why, when I run the following example, I only have Parallel.ForEach, run the number of threads equal to the number of cores on my machine? I thought that Parallel.ForEach gives you threads thread threads in which about 1000?

            int threads1;
            int threads2;

            ThreadPool.GetAvailableThreads(out threads1,out threads2);
            var list = Enumerable.Range(1, 200);
            var po = new ParallelOptions
            {
                MaxDegreeOfParallelism = 100
            };

            Parallel.ForEach(list, po, x =>
                {
                    Console.WriteLine("Thread:" + Thread.CurrentThread.ManagedThreadId);
                    Thread.Sleep(1000);
                });

Did I miss something?

+4
source share
2 answers

Parallel.ForEachuses a managed thread pool to schedule concurrent activities. The number of threads is set ThreadPool.SetMinThreadsand ThreadPool.SetMaxThreads. By default, the minimum number of threads is set to the number of processors in the system.

, . , .

MaxDegreeOfParallelism Parallel.For . , , .

, Thread.Sleep(100000);, .

ThreadPool.SetMinThreads(100, 100); Parallel.ForEach, 100, .

+6

, .

​​ . , , switch . - , .

IO, Task Paraller.For. Scott Hanselman.

Parallel.For , .

, TPL -, CLR #

+1

Source: https://habr.com/ru/post/1610023/


All Articles