我正在构建一个多线程 C# 应用程序,其中多个线程对队列中的元素起作用。单个线程正在使用同一个队列中的元素。我希望单线程对传入元素的元素进行一些减少/合并,因此理想情况下它会查看队列中的所有新元素,减少它们,然后在减少后处理条目。有点像这样:
while (true)
{
Collection<Elem> elements = queue.TakeAll();
Collection<Elem> reducedElements = Reduce(elements);
for (Elem e in reducedElements)
{
process(e);
}
}
但显然没有任何 TakeAll() 方法。从 Java 经验来看,我习惯了 BlockingQueue's drainTo method它提供了我感兴趣的内容。
我可以自己实现一些东西,只需使用 TryTake 直到队列为空。但这有生产线程可能也忙于生产的风险,这将导致没有有限的集合结束来减少和处理。我基本上是在寻找一种方法,将所有内容都从队列中取出,将其留空,但提供一个可以处理的集合。
最佳答案
查看命名空间 System.Collections.Concurrent 中的 ConcurrentQueue
。
此队列专为线程安全操作而设计。
您可以轻松地为您的目的添加一个扩展方法。
public static class Extensions
{
public static List<T> DrainTo<T>(this System.Collections.Concurrent.ConcurrentQueue<T> poConcurrentQueue)
{
List<T> loList = new List<T>();
T loElement;
while (poConcurrentQueue.TryDequeue(out loElement))
loList.Add(loElement);
return loList;
}
}
并像这样使用:
System.Collections.Concurrent.ConcurrentQueue<string> loConcurrentQueue = new System.Collections.Concurrent.ConcurrentQueue<string>();
loConcurrentQueue.Enqueue("Element1");
loConcurrentQueue.Enqueue("Element2");
var loList = loConcurrentQueue.DrainTo();
关于c# - 是否有 C# 等效于 Java 的 BlockingQueue.drainTo(Collection) 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42017079/