我有一个BatchBlock
,上面定义了BoundedCapacity
var _batchBlock = new BatchBlock<int>(2, new GroupingDataflowBlockOptions
{BoundedCapacity = 100 });
因此,如果队列容量达到 100, block 将推迟收到的每条消息,直到有可用的位置。 在这种情况下,批处理队列是贪婪的还是非贪婪的?
最佳答案
该 block 是贪婪的,但不是因为它如何处理 100 以上的项目,而是 2 以下的项目。您可以将贪婪值设置为 false
(true
是默认值),然后只有当有足够的元素可以发送一批时,该 block 才会实际消耗元素,直到那时它们才会被推迟:
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
关于c# - 定义有界容量的贪婪和非贪婪数据流 block 之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26394644/