【发布时间】:2015-05-14 20:33:00
【问题描述】:
在我的课堂上
public static ConcurrentDictionary<string, HashSet<string>> ConnectedUserConnections = new ConcurrentDictionary<string, HashSet<string>>();
添加或更新时,我应该通过以下方式更新:
ConnectedUserConnections.AddOrUpdate(userId, new HashSet<string>(), (key, existingVal) =>
{
existingVal.Add(connectionId);
return existingVal;
});
或
ConnectedUserConnections.AddOrUpdate(userId, new HashSet<string>(), (key, existingVal) =>
{
lock(ConnectedUserConnections)
{
existingVal.Add(connectionId);
return existingVal;
}
});
非常感谢大家。
【问题讨论】:
标签: c# .net .net-4.0 locking concurrentdictionary