更新ConcurrentDictionary中的值字段

我试图更新ConcurrentDictionary中的条目,如下所示:

class Class1 { public int Counter { get; set; } } class Test { private ConcurrentDictionary dict = new ConcurrentDictionary(); public void TestIt() { foreach (var foo in dict) { foo.Value.Counter = foo.Value.Counter + 1; // Simplified example } } } 

基本上我需要遍历字典并更新每个Value上的字段。 我从文档中了解到我需要避免使用Value属性。 相反,我认为我需要使用TryUpdate,除了我不想替换我的整个对象。 相反,我想更新对象上的字段。

在PFX团队博客上阅读此博客条目之后:也许我需要使用AddOrUpdate并且在添加委托中什么都不做。

有没有人对如何做到这一点有任何见解?


我在字典中有成千上万的对象,我需要每30秒左右更新一次。 创建新的以更新属性可能是不可行的。 我需要克隆现有对象,更新它并替换字典中的对象。 我还需要在克隆/添加周期的持续时间内锁定它。 呸。

我想做的是迭代对象并尽可能直接更新Counter属性。

我的最新研究让我看到了Parallel.ForEach,听起来不错,但它不应该用于更新状态的动作。

我也看到了Interlocked.Increment的提及听起来不错,但我仍然需要弄清楚如何在线程安全的方式中使用它在我的字典中的每个元素上。

首先,解决您的锁定问题:

 class Class1 { // this must be a variable so that we can pass it by ref into Interlocked.Increment. private int counter; public int Counter { get{return counter; } } public void Increment() { // this is about as thread safe as you can get. // From MSDN: Increments a specified variable and stores the result, as an atomic operation. Interlocked.Increment(ref counter); // you can return the result of Increment if you want the new value, //but DO NOT set the counter to the result :[ie counter = Interlocked.Increment(ref counter);] This will break the atomicity. } } 

迭代正确值应该比迭代键值对更快。 [虽然我认为在大多数情况下迭代一系列键并进行查看会更快地在ConcurrentDictionary上。]

 class Test { private ConcurrentDictionary dictionary = new ConcurrentDictionary(); public void TestIt() { foreach (var foo in dictionary.Values) { foo.Increment(); } } public void TestItParallel() { Parallel.ForEach(dictionary.Values,x=>x.Increment() ); } } 

ConcurrentDictionary并不能帮助您同时访问存储值的成员,只是元素本身。

如果多个线程调用TestIt,您应该获取集合的快照并锁定共享资源(这是单个字典值):

 foreach (KeyValuePair kvp in dict.ToArray()) { Class1 value = kvp.Value; lock (value) { value.Counter = value.Counter + 1; } } 

但是,如果要更新特定键的计数器,ConcurrentDictionary可以帮助您在键不存在时以primefaces方式添加新键值对:

 Class1 value = dict.GetOrAdd(42, key => new Class1()); lock (value) { value.Counter = value.Counter + 1; } 

AddOrUpdate和TryUpdate确实适用于您要在ConcurrentDictionary中替换给定键的值的情况。 但是,正如您所说,您不想更改值,您想要更改值的属性。