c# - 定义有界容量的贪婪和非贪婪数据流 block 之间的区别

标签 c# .net task-parallel-library tpl-dataflow

我有一个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

来自 Dataflow (Task Parallel Library)

关于c# - 定义有界容量的贪婪和非贪婪数据流 block 之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26394644/

相关文章:

c# - 传递 IOC 容器的替代方案

c# - 如何将不匹配的标签序列化到字典?

c# - 如何获取任务的结果或返回值?

c# - 对于表示返回 void 的操作的任务,Task.FromResult<T>() 的替代方法是什么

c# - Task.WaitAll 在所有任务完成之前返回

c# - Visual Studio 性能分析 - 找不到类库符号

C# String.Format - 无效的输入字符串

c# - 如何使用nest elasticsearch查询一种类型的数据限制

c# - 是否可以采取变通方法通过 ref 传递属性?加上路过引用事件

c# - Silverlight WCF RIA 从单个项目加载