生产者消费者具有单独BlockingCollection的单独类 [英] Producer Consumer Separate Classes with common BlockingCollection

查看:66
本文介绍了生产者消费者具有单独BlockingCollection的单独类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

希望有人可以提供有关生产者/消费者模式的一些建议,尤其是如何最好地实现对所有生产者/消费者类实例 COMMON 的Queue/BlockingCollection?

Hoping someone can give some advice regarding the Producer/Consumer Pattern – in particular on how best to achieve a Queue/BlockingCollection which is COMMON to all producers/consumer class instances?

让场景简化;认为我有;

Lets Simplify the scenario; Consider I have;

  • 单个Producer类
  • 一个单一的Consumer类.
  • 服务包含生产者和消费者实例的类班级.服务类只是告诉生产者/消费者开始和停止工作.

生产者将填充BlockingCollection

The producer will populate a BlockingCollection

消费者将需要从相同的BlockingCollection中读取内容

The consumer will need to read from that very same BlockingCollection

这很容易做到,如本文所展示;

This is all very easy to do as demonstrated in this article;

https://msdn.microsoft.com/zh-CN/library/dd287186.aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-2

此示例本质上在同一类中具有PRODUCER和CONSUMER,引用Common Queue/BlockingCollection当然很简单,因为对该对象的引用是同一类中的私有成员.

This example essentially has the PRODUCER and the CONSUMER within the same class, referencing a Common Queue/BlockingCollection is of course trivial as the reference to the object is to a private member within the same class.

如果我将生产者和消费者划分为不同的类,那么这就提出了一个问题,即如何拥有一个通用的BlockingCollection.

If I separate the Producer and Consumer into separate Classes, then this raises the question of how to have a common BlockingCollection.

我应该将服务类"设为静态/共享类,在此类中创建BlockingCollection并将其公开为朋友/公共成员吗?

Should I make the "Service Class" a Static/Shared Class, create the BlockingCollection in this class and expose it as a friend/public member?

我应该将公共队列放在哪里?

Where should I put the common Queue?

预先感谢!

推荐答案

只需设计您的 Producer Consumer 类以将 BlockingCollection 接受为构造函数参数.

Just design your Producer and Consumer classes to accept the BlockingCollection as a constructor parameter.

然后,无论您在何处实例化这些类,甚至可能实例化多个类,只需确保将 BlockingCollection 的相同实例传递给所有生产者和消费者.完成此操作后,无需保留对 BlockingCollection 的其他外部引用,除非您需要其他引用.每个 Producer Consumer 拥有对同一 BlockingCollection 实例的私有引用就足够了.

Then, wherever you instantiate these classes, and perhaps even more than one of each, just make sure to pass in the same instance of the BlockingCollection to all producers and consumers. Once you've done that, there is no need to keep some other external reference to the BlockingCollection, unless you need it for something else. It's enough that each Producer and Consumer hold a private reference to the same BlockingCollection instance.

基本上,它看起来像这样:

Basically, it would look something like this:

public class Producer {
    private BlockingCollection queue;

    public Producer(BlockingCollection queue) {
        this.queue = queue;
    }

    // more code...
}

public class Consumer {
    private BlockingCollection queue;

    public Consumer(BlockingCollection queue) {
        this.queue = queue;
    }

    // more code...
}

// The exact design of this class is up to you. This is just an example.
public class ProducerConsumerBuilder {
    public void BuildProducerAndConsumer(out Producer producer, out Consumer consumer) {
        BlockingCollection queue = new BlockingCollection();
        producer = new Producer(queue);
        consumer = new Consumer(queue);

        // No need to save a reference to "queue" here any longer,
        // unless you need it for something else.
        // It's enough that the producer and consumer each
        // hold their own private reference to the same queue.
    }
}

这篇关于生产者消费者具有单独BlockingCollection的单独类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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