【问题标题】:Is the list order of a ConcurrentDictionary guaranteed?ConcurrentDictionary 的列表顺序是否得到保证?
【发布时间】:2021-01-27 02:52:46
【问题描述】:

我使用 ConcurrentDictionary 来存储日志行,当我需要向用户显示它们时,我调用 ToList() 来生成一个列表。但奇怪的是,有些用户在列表中最先收到最新的行,而从逻辑上讲,他们应该排在最后。

这是因为 ConcurrentDictionary 不保证 IEnumerate 接口上的持久顺序,还是可能是什么原因?

【问题讨论】:

  • 显示数据时按日期时间值排序。
  • @usr 我正计划这样做,但这是一种解决方法。我想先了解它为什么会发生。

标签: .net list concurrency concurrentdictionary blockingcollection


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    没有。不保证 ConcurrentDictionary 的列表顺序,行可以以任意顺序出现。

    【讨论】:

      猜你喜欢
      • 2021-03-16
      • 2014-09-29
      • 2015-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-03
      相关资源
      最近更新 更多