是否可以确保ConcurrentDictionary的列表顺序? [英] Is the list order of a ConcurrentDictionary guaranteed?

查看:822
本文介绍了是否可以确保ConcurrentDictionary的列表顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ConcurrentDictionary存储日志行,当需要向用户显示日志行时,我致电ToList()生成列表.但是奇怪的是,某些用户在列表中排在第一位,而在逻辑上应该排在最后.

这是因为ConcurrentDictionary不保证IEnumerate接口上的持久性顺序吗?或者是什么原因?

解决方案

ConcurrentDictionary(和Dictionary<T>)不能保证列表中键的顺序.您将必须使用其他数据类型或自己执行排序.对于非并行代码,您可以使用 SortedDictionary<T> ,但我不认为并发集合中有类似的东西.

I am using a ConcurrentDictionary to store log-lines, and when I need to display them to the user I call ToList() to generate a list. But the weird thing is that some users receive the most recent lines first in the list, while they should logically be last.

Is this because ConcurrentDictionary doesnt guarantee a persistent order on the IEnumerate interface, or what can be the reason?

解决方案

No ConcurrentDictionary (and Dictionary<T> for that matter) does not guarantee the ordering of the keys in the list. You'll have to use a different data type or perform the sorting yourself. For non-concurrent code you would use SortedDictionary<T>, but I don't believe there is an analogue in the concurrent collections.

这篇关于是否可以确保ConcurrentDictionary的列表顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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