如何检查它是否包含堆栈中的特定对象? private ConcurrentStackint cs = new ConcurrentStackint();cs.Push(1); 方法 StackT.Contains 在 ConcurrentStackT -class中不可用.我想因为那不是线程安全的. 因此,如果您需
private ConcurrentStack<int> cs = new ConcurrentStack<int>(); cs.Push(1);方法
Stack<T>.Contains
在
ConcurrentStack<T>
-class中不可用.我想因为那不是线程安全的.
因此,如果您需要它,您必须使用锁,然后您可以使用Enumerable.Contains:
private ConcurrentStack<int> cs = new ConcurrentStack<int>(); private Object csLockObject = new Object();
…
bool contains = false; lock (csLockObject) { contains = cs.Contains(1); }
但是,当您枚举此快照时,另一个线程可能会向堆栈添加项目或从堆栈中删除项目.如果您想要防止在添加/删除的位置还需要锁定.
I want to avoid duplicates
好吧,你可以使用这样的类,它使用ConcurrentDictionary来检查它是否是唯一的:
public class ConcurrentUniqueStack<T> { private readonly ConcurrentDictionary<T, int> _itemUnique; // there is no ConcurrentHashSet so we need to use a Key-only dictionary private readonly ConcurrentStack<T> _stack; public ConcurrentUniqueStack() : this(EqualityComparer<T>.Default) { } public ConcurrentUniqueStack(IEqualityComparer<T> comparer) { _stack = new ConcurrentStack<T>(); _itemUnique = new ConcurrentDictionary<T, int>(comparer); } public bool TryPush(T item) { bool unique = _itemUnique.TryAdd(item, 1); if (unique) { _stack.Push(item); } return unique; } public bool TryPop(out T result) { bool couldBeRemoved = _stack.TryPop(out result); if (couldBeRemoved) { _itemUnique.TryRemove(result, out int whatever); } return couldBeRemoved; } public bool TryPeek(out T result) => _stack.TryPeek(out result); }