具有自动大小限制管理的 ThreadSafe FIFO 列表

ThreadSafe FIFO List with Automatic Size Limit Management(具有自动大小限制管理的 ThreadSafe FIFO 列表)

本文介绍了具有自动大小限制管理的 ThreadSafe FIFO 列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找出要使用的数据类型...基本上我想要一个线程安全的 FIFO 队列,一旦达到预先指定的限制,它将自动丢弃足够旧的项目.

I'm trying to figure out what data type to use... Basically I want a FIFO queue that is thread-safe and will automatically throw out old enough items once it gets to a pre-specified limit.

嗯,实际上,可能更多的是一个列表,因为我不想要将一个项目推入队列并从队列中弹出一个不再可用的项目的整个概念.

Well, actually, maybe more of a list, because I don't want the whole concept of pushing onto the queue and popping an item off the queue at which point it's no longer available.

该用例基本上是一个播放列表,其中我最多有 5 个即将播放的项目、当前播放的项目,然后是大约 20 个已经播放的项目.因此,为什么我猜它不能是一个队列,我将访问中间的一个项目作为当前"项目.当列表变大时,我宁愿不必手动管理丢弃旧项目......显然我可以自己编写这些,但如果 C# 已经存在,我不想重新发明轮子.

The use case is basically for a playlist where I would have up to 5 upcoming items, the currently playing item, and then about 20 items that have already played. Hence, why I guess it cannot be a queue, I would be accessing one of the items in the middle as the "current" item. And I would rather not have to manually manage throwing away old items when the list gets to big... obviously I could write this all myself, but I don't want to reinvent the wheel if this already exists for C#.

关于我可以使用什么的任何想法?

Any ideas of what I could use?

推荐答案

在框架中有一些东西几乎具有您想要的功能 - ConcurrentQueue .它是线程安全的队列,大多数操作都是无锁实现的,所以速度非常快.

In the Framework there is something almost having the functionality you want - the ConcurrentQueue . It is thread-safe queue with most operations being implemented lock-free, so it is very fast.

唯一没有的功能是自动丢弃"的限制"...

The only function is does not have is the "limit" with automatic "throw-away"...

但这可以很容易地添加 - 只需创建您自己的包含私有 ConcurrentQueue 的类,并通过出列/丢弃在您的公共入队方法中实现丢弃部分",直到您的限制得到满足在将新元素加入队列之前.

But that can be easily added - just create you own class containing a private ConcurrentQueue and implement the "throw-away part" in your public enqueue method by Dequeuing/throwing away till your limit is satisfied before Enqueuing the new element.

编辑 - 根据评论:
一种选择是使第二个队列"成为 ObservableCollection - 虽然本质上不是线程安全的(请注意),但这很容易在 WPF 中绑定......

EDIT - as per comment:
One option would be to make the second "Queue" an ObservableCollection - though not inherently thread-safe (beware) this would be easily bind in WPF...

另一个方法是让你的类实现 ObservableCollection 相应的接口(由 IList<T>、ICollection<T>、IEnumerable<T>、IList、ICollection、IEnumerable、INotifyCollectionChanged、INotifyPropertyChanged 组成)——听起来很多,但大多数通过中继到内部 ConcurrentQueue 可以轻松实现其中的一些,因此无需编写太多真正的代码...
请参阅 http://msdn.microsoft.com/en-us/library/ms752347.aspx

Another would be to make your class implement the ObservableCollection interface (which consists of IList<T>, ICollection<T>, IEnumerable<T>, IList, ICollection, IEnumerable, INotifyCollectionChanged, INotifyPropertyChanged) accordingly - that sounds alot, but most of these you get easily implemented by relaying to the internal ConcurrentQueue so there is not much real code to write...
See http://msdn.microsoft.com/en-us/library/ms752347.aspx

这篇关于具有自动大小限制管理的 ThreadSafe FIFO 列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:具有自动大小限制管理的 ThreadSafe FIFO 列表

基础教程推荐