Why does iterating over GetConsumingEnumerable() not fully empty the underlying blocking collection

前端 未结 4 1940
天涯浪人
天涯浪人 2021-02-04 04:16

I have a quantifiable & repeatable problem using the Task Parallel Library, BlockingCollection, ConcurrentQueue & GetCo

4条回答
  •  醉话见心
    2021-02-04 04:35

    I couldn't replicate your behavior with simple console application doing basically the same thing (running on .Net 4.5 beta, which could make a difference). But I think the reason this happens is that Parallel.ForEach() tries to optimize execution by splitting the input collection into chunks. And with your enumerable, a chunk can't be created until you add more items to the collection. For more information, see Custom Partitioners for PLINQ and TPL on MSDN.

    To fix this, don't use Parallel.ForEach(). If you still want to process the items in parallel, you can start a Task in each iteration.

提交回复
热议问题