【发布时间】:2017-12-19 20:57:56
【问题描述】:
我需要以线程安全的方式来获取 ConcurrentDictionary(string, List) 的每一项
使用以下结构是否安全?
foreach (var item in concurrentDict)
{
var readonlyCollection = item.Value.AsReadOnly();
//use readonly collection, (I need only Values of the concurrent dictionary)
}
我想使用字典安全的值。
【问题讨论】:
-
如果除了阅读什么都不做,会有什么危险呢?只有在读写时才会遇到线程问题。
-
谢谢。我可以使用 foreach (var dictValue in concurrentDictionary.Values) { foreach(item in dictValue) { } } 吗?
-
只需尝试并找出答案(是的,您可以)花费更少的时间