是不是很好用BlockingCollection< T>作为单生产,单消费FIFO查询? [英] is it good to use BlockingCollection<T> as single-producer, single-consumer FIFO query?

查看:182
本文介绍了是不是很好用BlockingCollection< T>作为单生产,单消费FIFO查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要单生产,单消费FIFO查询,因为

I need single-producer, single-consumer FIFO query because

  • 我需要处理在他们收到的订单消息。
  • 我要做到这一点异步,因为当我处理邮件呼叫者不应该等待。
  • 在接下来的消息处理应在previous消息处理完毕才开始。有时,接收消息的频率比处理信息的频率更高。但在平均,我应该能够处理所有的消息,只是有时候我不得不排队包装他们。

因此​​,它是pretty的如TCP / IP,我认为,那就是你有一个生产者和一个消费者,有时你能比你快可以处理收到的消息,所以你必须对它们进行查询。凡顺序很重要并在主叫完全不感兴趣,你在做什么用的东西。

So it's pretty like TCP/IP I think, where you have one producer and one consumer, SOMETIMES you can receive messages faster than you can process, so you have to query them. Where order IS important and where caller absolutely not interested what you doing with that stuff.

这听起来很简单,我可能可以使用一般的队列对于这一点,但我想用 BlockingCollection 的因为我不想写任何code与的ManualResetEvent 等。

This sounds easy enough and I likely can use general Queue for that, but I want to use BlockingCollection for that because I don't want to write any code with ManualResetEvent etc.

如何适用 BlockingCollection 我的任务,也许你可以建议其他的东西?

How suitable BlockingCollection for my task and probably you can suggest something else?

推荐答案

BlockingCollection 类实现的 IProducerConsumerCollection 界面,完全符合您的要求。

BlockingCollection class implements IProducerConsumerCollection interface so perfectly fits your requirements.

您可以创建两个任务,一个是异步生产者和另一个消费工人。前者将项目添加到 BlockingCollection ,后者只想尽快消费新在FIFO的顺序使用。

You can create two Tasks, one for async producer and an other one as consumer worker. Former would add items to BlockingCollection and the latter just consume as soon as new are available in FIFO order.

class ProducerConsumer
{
    private static BlockingCollection<string> queue = new BlockingCollection<string>();

    static void Main(string[] args)
    {
        Start();
    }

    public static void Start()
    {
        var producerWorker = Task.Factory.StartNew(() => RunProducer());
        var consumerWorker = Task.Factory.StartNew(() => RunConsumer());

        Task.WaitAll(producerWorker, consumerWorker);
    }

    private static void RunProducer()
    {
        int itemsCount = 100;

        while (itemsCount-- > 0)
        {
            queue.Add(itemsCount + " - " + Guid.NewGuid().ToString());
            Thread.Sleep(250);
        }
    }

    private static void RunConsumer()
    {
        foreach (var item in queue.GetConsumingEnumerable())
        {
           Console.WriteLine(DateTime.Now.ToString("HH:mm:ss.ffff") + " | " + item);
        }
    }
}

IProducerConsumerCollection

定义的方法来操作用于线程安全的集合   生产者/消费者的使用。该接口提供一个统一的   再presentation的生产者/消费者的集合,这样较高的水平   抽象,例如   System.Collections.Concurrent.BlockingCollection(Of T)中可以使用   收藏作为底层存储机制。

Defines methods to manipulate thread-safe collections intended for producer/consumer usage. This interface provides a unified representation for producer/consumer collections so that higher level abstractions such as System.Collections.Concurrent.BlockingCollection(Of T) can use the collection as the underlying storage mechanism.

这篇关于是不是很好用BlockingCollection&LT; T&GT;作为单生产,单消费FIFO查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆