none
More threads are spawned than MaxDegreeOfParallelism in ParallelForEach RRS feed

  • Question

  • Hi Team,

    I am using Parallel.ForEach with ParallelOption having MaxDegreeOfParallelism set to processor count.


    Parallel.ForEach(reqs, parallelOptions, req => ProcesRequest(req));

    Inside ProcessRequest method I am opening Executable. This process does some business logic. 

    However I observed that, number of executable opened in task Manager are more than processor count (MaxDegreeOfParallelism count).

    Could you please let me know why thread count is more than processor count?

    As per my understanding thread spawned must be less than MaxDegreeOfParallelism. 

    Please let me know feedback on above understanding.

    What corrective measure shall I apply?

    Is there any better design than Parallel.ForEach?





    • Edited by Omkaraaa Friday, June 1, 2018 5:56 PM
    Friday, June 1, 2018 5:52 PM

All replies

  • Hi Omkaraaa,

    Could you share us a simple project which could reproduce your issue?

    I made a test with code below, but I fail to reproduce your issue.

                List<string> source = new List<string>();
                for (int i = 0; i < 10; i++)
                {
                    source.Add($"Source {i.ToString()}");
                }
                Parallel.ForEach(source,new ParallelOptions { MaxDegreeOfParallelism = 2 }, s => {
                    Console.WriteLine(s + " " + Thread.CurrentThread.ManagedThreadId);
                    Thread.Sleep(1000);
                });
    
     

    The concurrency Thread did not exceed the 2.

    Best Regards,

    Tao Zhou


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, June 4, 2018 2:41 AM