您的实现是正确的。不幸的是,.NET framework没有提供内置的并发哈希集类型。但是,有一些解决方法。
ConcurrentDictionary(推荐)
第一个是使用
ConcurrentDictionary<TKey,TValue>命名空间中的类
System.Collections.Concurrent。在这种情况下,该值是无意义的,因此我们可以使用一个简单的
byte(内存中为1个字节)。
private ConcurrentDictionary<string, byte> _data;
推荐使用此选项,因为该类型是线程安全的,并且
HashSet<T>除了键和值是不同的对象之外,还为您提供了相同的优点。
来源:社交MSDN
并发袋
如果您不介意重复的条目,则可以
ConcurrentBag<T>在上一个类的相同命名空间中使用该类。
private ConcurrentBag<string> _data;
自我实现
最后,像您所做的那样,您可以使用锁或.NET为您提供线程安全的其他方式来实现自己的数据类型。这是一个很好的例子:如何在.Net中实现ConcurrentHashSet
该解决方案的唯一缺点是
HashSet<T>,即使对于读取操作,该类型也无法进行正式的并发访问。
我引用链接文章的代码(最初由Ben Mosher编写)。
using System.Collections.Generic;using System.Threading;namespace BlahBlah.Utilities{ public class ConcurrentHashSet<T> : IDisposable { private readonly ReaderWriterLockSlim _lock = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion); private readonly HashSet<T> _hashSet = new HashSet<T>(); #region Implementation of ICollection<T> ...ish public bool Add(T item) { _lock.EnterWriteLock(); try { return _hashSet.Add(item); } finally { if (_lock.IsWriteLockHeld) _lock.ExitWriteLock(); } } public void Clear() { _lock.EnterWriteLock(); try { _hashSet.Clear(); } finally { if (_lock.IsWriteLockHeld) _lock.ExitWriteLock(); } } public bool Contains(T item) { _lock.EnterReadLock(); try { return _hashSet.Contains(item); } finally { if (_lock.IsReadLockHeld) _lock.ExitReadLock(); } } public bool Remove(T item) { _lock.EnterWriteLock(); try { return _hashSet.Remove(item); } finally { if (_lock.IsWriteLockHeld) _lock.ExitWriteLock(); } } public int Count { get { _lock.EnterReadLock(); try { return _hashSet.Count; } finally { if (_lock.IsReadLockHeld) _lock.ExitReadLock(); } } } #endregion #region Dispose public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } protected virtual void Dispose(bool disposing) { if (disposing) if (_lock != null) _lock.Dispose(); } ~ConcurrentHashSet() { Dispose(false); } #endregion }}编辑: 移动入口锁定方法将
try块移开,因为它们可能引发异常并执行
finally块中包含的指令。



