【问题标题】:C#, ConcurrentQueue size limit and time frameC#,ConcurrentQueue 大小限制和时间范围
【发布时间】:2019-06-13 15:13:36
【问题描述】:

我正在寻找一种方法: 1. 从限制一定大小的 ConcurentQueue 读取消息。 2. 一次阅读不超过 X 条消息。 一旦 2 个命中之一,我想停止从 Q 读取, 直到其他代码完成并再次执行相同的操作。

我看到队列溢出的不同实现, 在这里Fixed size queue which automatically dequeues old values upon new enques 但可以 不知道如何正确组合它们。

public class FixedSizedQueue<T>

public int Size { get; private set; }

public FixedSizedQueue(int size)
{
    Size = size;
}

public void Enqueue(T obj)
{
    queue.Enqueue(obj);

    while (queue.Count > Size)
    {
        T outObj;
        queue.TryDequeue(out outObj);
    }
}

}

【问题讨论】:

    标签: c# queue concurrent-queue


    【解决方案1】:

    尝试使用BlockingCollection 而不是ConcurrentQueue

    https://docs.microsoft.com/en-us/dotnet/api/system.collections.concurrent.blockingcollection-1?view=netframework-4.8

    当您尝试在集合中添加另一个元素(如果已满)时,它会等到一个元素被占用。

    【讨论】:

    • 与 concurrentqueue 我可以使用 isempty 来检查差异在哪里?
    猜你喜欢
    • 1970-01-01
    • 2012-05-13
    • 2019-06-30
    • 1970-01-01
    • 2014-03-22
    • 1970-01-01
    • 1970-01-01
    • 2012-06-24
    • 1970-01-01
    相关资源
    最近更新 更多