Concurrent Collections and Unique elements

2019-06-21 20:13发布

I have a concurrent BlockingCollection with repeated elements. How can modify it to add or get distinct elements?

1条回答
倾城 Initia
2楼-- · 2019-06-21 20:39

The default backing store for BlockingCollection is a ConcurrentQueue. As somebody else pointed out, it's rather difficult to add distinct items using that.

However, you could create your own collection type that implements IProducerConsumerCollection, and pass that to the BlockingCollection constructor.

Imagine a ConcurrentDictionary that contains the keys of the items that are currently in the queue. To add an item, you call TryAdd on the dictionary first, and if the item isn't in the dictionary you add it, and also add it to the queue. Take (and TryTake) get the next item from the queue, remove it from the dictionary, and return.

I'd prefer if there was a concurrent HashTable, but since there isn't one, you'll have to do with ConcurrentDictionary.

查看更多
登录 后发表回答