c# – 定义了boundedcapacity的贪婪和非贪婪数据流块之间的差异

前端之家收集整理的这篇文章主要介绍了c# – 定义了boundedcapacity的贪婪和非贪婪数据流块之间的差异前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个定义了BoundedCapacity的BatchBlock
var _batchBlock = new BatchBlock<int>(2,new GroupingDataflowBlockOptions
                                      {BoundedCapacity = 100 });

因此,如果队列容量达到100,则该块会推迟收到的每条消息,直到某个地点可用.
在这种情况下,批处理队列被认为是贪婪还是非贪婪?

解决方法

该块是贪婪的,但不是因为它如何处理100以上的项目,而是因为2以下的项目.您可以将贪婪值设置为false(默认值为true)然后块只会在有足够数量时实际使用项目发送一批,直到那时它们被推迟:
var batchBlock = new BatchBlock<int>(2,new GroupingDataflowBlockOptions
{
    Greedy = false,BoundedCapacity = 100.
});

The BatchBlock class operates in either greedy or non-greedy mode. In greedy mode,which is the default,a BatchBlock object accepts every message that it is offered and propagates out an array after it receives the specified count of elements. In non-greedy mode,a BatchBlock object postpones all incoming messages until enough sources have offered messages to the block to form a batch

Dataflow (Task Parallel Library)

原文链接:https://www.f2er.com/csharp/244771.html

猜你在找的C#相关文章