如何在.Net中实现ConcurrentHashSet
我正在尝试本着 ConcurrentDictionary 的精神实现 ConcurrentHashSet, 采取的方法是使用内部支持 ConcurrentDictionary 并编写小型委托方法,这就是我所取得的进展,但是我坚持使用集合理论方法,尤其是。我不确定是否可以使用 foreach 并且仍然不违反并发性
public class ConcurrentHashSet<TElement> : ISet<TElement>
{
private readonly ConcurrentDictionary<TElement, object> _internal;
public ConcurrentHashSet(IEnumerable<TElement> elements = null)
{
_internal = new ConcurrentDictionary<TElement, object>();
if (elements != null)
UnionWith(elements);
}
public void UnionWith(IEnumerable<TElement> other)
{
if (other == null) throw new ArgumentNullException("other");
foreach (var otherElement in other)
Add(otherElement);
}
public void IntersectWith(IEnumerable<TElement> other)
{
throw new NotImplementedException();
}
public void ExceptWith(IEnumerable<TElement> other)
{
throw new NotImplementedException();
}
public void SymmetricExceptWith(IEnumerable<TElement> other)
{
throw new NotImplementedException();
}
public bool IsSubsetOf(IEnumerable<TElement> other)
{
throw new NotImplementedException();
}
public bool IsSupersetOf(IEnumerable<TElement> other)
{
throw new NotImplementedException();
}
public bool IsProperSupersetOf(IEnumerable<TElement> other)
{
throw new NotImplementedException();
}
public bool IsProperSubsetOf(IEnumerable<TElement> other)
{
throw new NotImplementedException();
}
public bool Overlaps(IEnumerable<TElement> other)
{
return other.Any(otherElement => _internal.ContainsKey(otherElement));
}
public bool SetEquals(IEnumerable<TElement> other)
{
int otherCount = 0;
int thisCount = Count;
foreach (var otherElement in other)
{
otherCount++;
if (!_internal.ContainsKey(otherElement))
return false;
}
return otherCount == thisCount;
}
public bool Add(TElement item)
{
return _internal.TryAdd(item, null);
}
public void Clear()
{
_internal.Clear();
}
// I am not sure here if that fullfills contract correctly
void ICollection<TElement>.Add(TElement item)
{
Add(item);
}
public bool Contains(TElement item)
{
return _internal.ContainsKey(item);
}
public void CopyTo(TElement[] array, int arrayIndex)
{
_internal.Keys.CopyTo(array, arrayIndex);
}
public bool Remove(TElement item)
{
object ignore;
return _internal.TryRemove(item, out ignore);
}
public int Count
{
get { return _internal.Count; }
}
public bool IsReadOnly
{
get { return false; }
}
public IEnumerator<TElement> GetEnumerator()
{
return _internal.Keys.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
I am trying to implement a ConcurrentHashSet in the spirit of ConcurrentDictionary,
approach taken is to use a internal backing ConcurrentDictionary and write small delegating methods, this is how far i got, but well the set theoretic methods are I am stuck on, esp. I am not sure if I can use a foreach and still not violate concurrency
public class ConcurrentHashSet<TElement> : ISet<TElement>
{
private readonly ConcurrentDictionary<TElement, object> _internal;
public ConcurrentHashSet(IEnumerable<TElement> elements = null)
{
_internal = new ConcurrentDictionary<TElement, object>();
if (elements != null)
UnionWith(elements);
}
public void UnionWith(IEnumerable<TElement> other)
{
if (other == null) throw new ArgumentNullException("other");
foreach (var otherElement in other)
Add(otherElement);
}
public void IntersectWith(IEnumerable<TElement> other)
{
throw new NotImplementedException();
}
public void ExceptWith(IEnumerable<TElement> other)
{
throw new NotImplementedException();
}
public void SymmetricExceptWith(IEnumerable<TElement> other)
{
throw new NotImplementedException();
}
public bool IsSubsetOf(IEnumerable<TElement> other)
{
throw new NotImplementedException();
}
public bool IsSupersetOf(IEnumerable<TElement> other)
{
throw new NotImplementedException();
}
public bool IsProperSupersetOf(IEnumerable<TElement> other)
{
throw new NotImplementedException();
}
public bool IsProperSubsetOf(IEnumerable<TElement> other)
{
throw new NotImplementedException();
}
public bool Overlaps(IEnumerable<TElement> other)
{
return other.Any(otherElement => _internal.ContainsKey(otherElement));
}
public bool SetEquals(IEnumerable<TElement> other)
{
int otherCount = 0;
int thisCount = Count;
foreach (var otherElement in other)
{
otherCount++;
if (!_internal.ContainsKey(otherElement))
return false;
}
return otherCount == thisCount;
}
public bool Add(TElement item)
{
return _internal.TryAdd(item, null);
}
public void Clear()
{
_internal.Clear();
}
// I am not sure here if that fullfills contract correctly
void ICollection<TElement>.Add(TElement item)
{
Add(item);
}
public bool Contains(TElement item)
{
return _internal.ContainsKey(item);
}
public void CopyTo(TElement[] array, int arrayIndex)
{
_internal.Keys.CopyTo(array, arrayIndex);
}
public bool Remove(TElement item)
{
object ignore;
return _internal.TryRemove(item, out ignore);
}
public int Count
{
get { return _internal.Count; }
}
public bool IsReadOnly
{
get { return false; }
}
public IEnumerator<TElement> GetEnumerator()
{
return _internal.Keys.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我刚刚遇到了类似的场景(“我对快速添加、包含和删除感兴趣”)并实现了这个傻瓜:
还没有真正测试过它(性能或可靠性方面)。 YMMV。
I just ran into a similar scenario ("I am interested in a fast Add and Contains and Remove") and implemented this sucker:
Haven't really tested it (performance- or reliability-wise). YMMV.
以下是基于 ConcurrentDictionary 的并发集的实现:
Here's an implementation of a concurrent set based on the
ConcurrentDictionary
:ConcurrentDictionary 具有更好的性能特征,因为它使用无锁方式进行读取(至少在 .NET 4.0+ 中)。因此,对于繁重的多线程场景中的性能,ConcurrentDictionary 作为 readerwriterlockslim 包装器可能会表现得更好。但是您需要携带一个空字节作为虚拟值(我同意,这看起来很糟糕)。
ConcurrentDictionary has better performance characteristics as it uses a lockfree manner for reads (at least in .NET 4.0+). So for performance in heavy mulithreaded scenarios a ConcurrentDictionary will probably perform better as a readerwriterlockslim wrapper. But you need to carry around an empty byte as dummyvalue (I agree, that looks awful).
你打算用它做什么?
即使您确实让这些方法起作用,您也可能会遇到以下情况:
这可能是可以接受的,但 Set 在并发设置中比 Queue 更不可能有用。
What do you intend to use it for?
Even if you do get the methods to work, you could have the following scenario:
That may be acceptable but a Set just is much less likely to be useful in a concurrent setting than a Queue.