Why isn't Parallel.ForEach running multiple threads?

后端 未结 6 2002
离开以前
离开以前 2020-12-15 17:24

Today i tried do some optimization to foreach statement, that works on XDocument.

Before optimization:

foreach (XElement el         


        
相关标签:
6条回答
  • 2020-12-15 17:41

    It's by design that Parallel.ForEach may use fewer threads than requested to achieve better performance. According to MSDN [link]:

    By default, the Parallel.ForEach and Parallel.For methods can use a variable number of tasks. That's why, for example, the ParallelOptions class has a MaxDegreeOfParallelism property instead of a "MinDegreeOfParallelism" property. The idea is that the system can use fewer threads than requested to process a loop.

    The .NET thread pool adapts dynamically to changing workloads by allowing the number of worker threads for parallel tasks to change over time. At run time, the system observes whether increasing the number of threads improves or degrades overall throughput and adjusts the number of worker threads accordingly.

    0 讨论(0)
  • 2020-12-15 17:46

    Not always the parallel way is faster than the "old fashion way" http://social.msdn.microsoft.com/Forums/en-US/parallelextensions/thread/c860cf3f-f7a6-46b5-8a07-ca2f413258dd

    0 讨论(0)
  • 2020-12-15 17:54

    use it like this:

    int ParallelThreads = 10;
    Parallel.ForEach(xDoc.Descendants("APSEvent").ToList(), new ParallelOptions() { MaxDegreeOfParallelism = ParallelThreads }, (myXDOC, i, j) =>
    {
     //do whatever you want here 
    });
    
    0 讨论(0)
  • 2020-12-15 18:00

    From the problem description, there is nothing that explains why the TPL is not spawning more threads.

    There is no evidence in the question that is even the problem. That can be fixed quite easily: you could log the thread id, before you enter the loop, and as the first thing you do inside your loop.

    If it is always the same number, it is the TPL failing to spawn threads. You should then try different versions of your code and what change triggers the TPL to serialize everything. One reason could be if there are a small number of elements in your list. The TPL partitions your collection, and if you have only a few items, you might end up with only one batch. This behavior is configurable by the way.

    It could be you are inadvertedly taking a lock in in the loop, then you will be seeing lots of different numbers, but no speedup. Then, simplify the code until the problem vanishes.

    0 讨论(0)
  • 2020-12-15 18:05

    Yes exactly, Document.Load(...) locks the file and due to resource contention between threads, TPL is unable to use the power of multiple threads. Try to load the XML into a Stream and then use Parallel.For(...).

    0 讨论(0)
  • 2020-12-15 18:06

    Do you happen to have a single processor? TPL may limit the number of threads to one in this case. Same thing may happen if the collection is very small. Try a bigger collection. See this answer for more details on how the degree of parallelism is determined.

    0 讨论(0)
提交回复
热议问题