gpt4 book ai didi

c# - BoundedCapacity 是否包括当前正在 TPL 数据流中处理的项目?

转载 作者:太空狗 更新时间:2023-10-29 20:13:22 25 4
gpt4 key购买 nike

BoundedCapacity 限制是否只包括输入队列中等待处理的项目,还是它也计算当前正在处理的项目?

让我们以这个ActionBlock为例:

var block = new ActionBlock<int>(
i => Console.WriteLine(i),
new ExecutionDataflowBlockOptions
{
BoundedCapacity = 1000,
MaxDegreeOfParallelism = 10,
});

如果当前有 5 个项目正在并行处理。这是否意味着输入队列可以在这些之上容纳 1000 个项目,或者只是 995 个?

最佳答案

显然,BoundedCapacity 确实包括了正在处理的项目,这些项目位于输入队列中等待的项目之上。这可以通过具有相同 ExecutionDataflowBlockOptions 和永远不会完成的 actionActionBlock 轻松演示:

var block = new ActionBlock<int>(
_ => Task.Delay(-1),
new ExecutionDataflowBlockOptions
{
BoundedCapacity = 1000,
MaxDegreeOfParallelism = 10,
});

for (int i = 0; i < 1001; i++)
{
Console.WriteLine("#{0} - InputCount={1}", i, block.InputCount);
await block.SendAsync(i);
}

输出如下,然后应用程序将无限期阻塞:

...
...
#990 - InputCount=980
#991 - InputCount=981
#992 - InputCount=982
#993 - InputCount=983
#994 - InputCount=984
#995 - InputCount=985
#996 - InputCount=986
#997 - InputCount=987
#998 - InputCount=988
#999 - InputCount=989
#1000 - InputCount=990

这是因为添加了 1000 个项目,其中 10 个 (MaxDegreeOfParallelism) 正在并发处理,其他 990 个正在输入队列中等待,第 1001 个st 项目可以永远不要进去。

关于c# - BoundedCapacity 是否包括当前正在 TPL 数据流中处理的项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26594604/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com