如何创建无重复ConcurrentQueue?ConcurrentQueue

由网友(爱上孤独)分享简介:我需要一个concurent集合,不允许重复(在BlockingCollection使用作为生产者/消费者)。我并不需要严格的顺序元素。从另一个的手,我希望尽量减少元素活在收集的最长时间。即集合不得后进先出法,理想情况下应该做到先进先出。I need a concurent collection that doe...

我需要一个concurent集合,不允许重复(在BlockingCollection使用作为生产者/消费者)。 我并不需要严格的顺序元素。 从另一个的手,我希望尽量减少元素活在收集的最长时间。即集合不得后进先出法,理想情况下应该做到先进先出。

I need a concurent collection that doesn't allow duplicates (to use in BlockingCollection as Producer/Consumer). I don't need strict order of elements. From another hand i want to minimize the maximum time of element "live" in collection. I.e. collection mustn't be LIFO, ideally it should be FIFO.

嗯,我会说我需要ConcurrentQueue,没有重复允许的,但ConcurrentBag的不重复也可以正常工作。

Well I would say that I need ConcurrentQueue with no duplicates allowed, but ConcurrentBag with no duplicates also might work.

为什么C#不包含这样的,事情可能别人已经创建了吗?

Why C# doesn't contain anything like that and probably someone already created it?

这个问题是我的previous问题What类型IProducerConsumerCollection&LT的; T>使用我的任务是什么?

This question is result of my previous question What type of IProducerConsumerCollection<T> to use for my task?

推荐答案

有结合了这套规则的集合没有内置的.NET库。你有三个选择:

There are no built-in .Net libraries that combine this set of rules for a collection. You have three options:

在写自己的集合类 使用两个集合:编写使用一个ConcurrentQueue和任何一组为基础收集的自定义类,自动检查重复;有加设置来看,如果成功,加入到ConcurrentQueue;每添加/删除将同时添加到收藏成功时 使用ConcurrentQueue但遍历整个列表检查重复

最后两个都不是很有效的(一个存储器,其他与CPU,I / O,锁定),并且由于需要对显式锁定混乱,但将完成的任务。他们将更快地实现,但如果权衡不能满足你的要求,你就必须去与选项#1。

The last two aren't very efficient (one with memory, the other with CPU, I/O, locking) and are messier because of the need for explicit locking, but would accomplish the task. They will be quicker to implement, but if the trade-offs don't meet your requirements you'll have to go with option #1.

阅读全文

相关推荐

最新文章