c# - C# BlockingCollection 的 TryTakeFromAny 是否保证顺序检查 BlockingCollection 对象?

标签 c# multithreading

我有两个阻塞集合 - 一个优先级高于另一个。如果我使用 TryTakeFromAny 并首先指定更高优先级的 BlockingCollection,是否可以保证首先查看更高优先级的队列?

最佳答案

这没有记录在案,所以我想说不能保证它将来不会改变。可能不建议长期依赖它。然而,目前,BlockingCollection<T>.TryTakeFromAny通过索引检查项目(它检查 Count > 0 然后执行 TryTake )循环遍历所有集合来进行快速检查。如果没有找到项目,它会为每个集合获取一个内部等待句柄并将它们传递给 WaitHandle.WaitAny .这提供了保证:

This method returns when any handle is signaled. If more than one object becomes signaled during the call, the return value is the array index of the signaled object with the smallest index value of all the signaled objects.

因此当前的实现确实会按要求运行。如果两个集契约(Contract)时获得一个项目,则将采用较低索引的项目。

关于c# - C# BlockingCollection 的 TryTakeFromAny 是否保证顺序检查 BlockingCollection 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26472251/

相关文章:

c# - 使用 jquery/ajax 在同一解决方案的另一个项目中调用 c#(或任何其他 .net)函数

Java 和多核

Java 并发迭代 : Divide and Conquer vs Runnable for each item

java - 抽象 Servlet - 这个方法是线程安全的吗?

python - 从 tkinter gui 停止 python 线程

c# - 从 VAT eu 解析一个 json

c# - 如何绑定(bind)可观察集合以查看可观察集合中存在的项目数?

c# - 绑定(bind)到 xpath 时 Mediaelement repeatbehavior 失败

c# - 通过 C# 设置 Excel 命名范围?

在c中为linux创建一个新线程?