【问题标题】:How to add a new element to a hashset that is value of a ConcurrentDictionary?如何将新元素添加到作为 ConcurrentDictionary 值的哈希集中?
【发布时间】:2017-10-31 10:26:05
【问题描述】:

我有一个 ConcurrentDictionary,它的 key 为 long,value 为 int 的 hashset。我希望如果该键不在字典中,则使用第一个元素添加一个新的哈希集。如果键存在,则将新元素添加到现有字典中。

我正在尝试这样的事情:

ConcurrentDictionary<long, HashSet<int>> myDic = new ConcurrentDictionary<long, HashSet<int>>();
int myElement = 1;
myDic.AddOrUpdate(1, new Hashset<int>(){myFirstElement},
(key, actualValue) => actualValue.Add(myElement));

这段代码的问题在于第三个参数,因为 .Add() 方法返回一个布尔值,而 AddOrUpdate 需要一个哈希集。第一个参数和第二个参数是对的。

所以我的问题是如何以线程安全的方式将新元素添加到哈希集中并避免重复(这就是我使用哈希集作为值的原因)。哈希集的问题是它不是线程安全的,如果我先得到它然后添加新元素,我在字典之外做,我可能会遇到问题。

谢谢。

【问题讨论】:

    标签: c# concurrentdictionary


    【解决方案1】:

    要修复编译器错误,您可以这样做:

    myDic.AddOrUpdate(1, new HashSet<int>() { myFirstElement },
        (key, actualValue) => {
            actualValue.Add(myFirstElement);
            return actualValue;
        });
    

    但是这不是线程安全的,因为“更新”功能不在任何锁内运行,因此您可能会从多个线程添加到非线程安全的HashSet。这可能会导致(例如)丢失值(例如,您将 1000 个项目添加到 HashSet 但最后您只有 970 个项目)。 AddOrUpdate 中的更新功能不应该有任何副作用,这里确实有。

    您可以锁定自己,无法向HashSet 添加值:

    myDic.AddOrUpdate(1, new HashSet<int>() { myFirstElement },
        (key, actualValue) => {
            lock (actualValue) {
                actualValue.Add(myFirstElement);
                return actualValue;
            }
        });
    

    但是问题是你为什么首先使用无锁结构(ConcurrentDictionary)。除此之外 - 任何其他代码都可能从您的字典中获得HashSet 并在没有任何锁的情况下在那里添加价值,从而使整个事情变得无用。因此,如果您出于某种原因决定采用这种方式 - 在从该字典访问 HashSet 时,您必须确保所有代码锁定。

    而不是所有这些 - 只需使用并发收集而不是 HashSet。据我所知,没有ConcurrentHashSet,但您可以使用另一个带有虚拟键的ConcurrentDictionary 作为替代品(或在互联网上查看自定义实现)。

    旁注。这里

    myDic.AddOrUpdate(1, new Hashset<int>(){myFirstElement}, 
    

    每次调用AddOrUpdate 时都会创建新的HashSet,即使不需要该字典,因为密钥已经存在。而是使用带有增值工厂的重载:

    myDic.AddOrUpdate(1, (key) => new HashSet<int>() { myFirstElement },
    

    编辑:ConcurrentDictionary 作为哈希集的示例用法:

    var myDic = new ConcurrentDictionary<long, ConcurrentDictionary<int, byte>>();
    long key = 1;
    int element = 1;
    var hashSet = myDic.AddOrUpdate(key, 
        _ => new ConcurrentDictionary<int, byte>(new[] {new KeyValuePair<int, byte>(element, 0)}),
        (_, oldValue) => {
            oldValue.TryAdd(element, 0);
            return oldValue;
        });
    

    【讨论】:

    • 非常感谢您的提示。你能举一个例子,我如何使用 CouncurrentDictionary 作为虚拟键的值?谢谢。
    • 在这个例子中。在我得到哈希集和尝试添加新元素之间,有人可能会删除它,所以当我尝试添加时可能会出错。或者至少我会将一个元素添加到字典中没有的哈希集.对吗?
    • @ÁlvaroGarcía 是的,这是正确的(没有错误,但您将不再添加到 hashset)。如果这对您来说是个问题 - 我认为您可以像原来一样使用 AddOrUpdate(当然使用 ConcurrentDictionary 而不是 HashSet)。
    • 我在想,用你的旁注做第一种方法,改变字典而不是哈希集。非常感谢。
    【解决方案2】:

    如果将匿名函数定义包裹在花括号中,则可以在函数体中定义多个语句,从而像这样指定返回值:

    myDic.AddOrUpdate(1, new HashSet<int>() { myFirstElement },
    (key, actualValue) => {
        actualValue.Add(myElement);
        return actualValue;
    });
    

    【讨论】:

      猜你喜欢
      • 2015-12-03
      • 2017-04-12
      • 2012-11-08
      • 2021-09-15
      • 2020-04-19
      • 2016-10-26
      • 2020-12-08
      • 2016-03-16
      • 2017-10-24
      相关资源
      最近更新 更多