在迭代 ConcurrentSkipListSet
时删除和添加元素是否安全:
ConcurrentSkipListSet<Element> set = new ConcurrentSkipListSet<Element>(myComparator);
for(Element e : set)
{
if(condition)
{
set.remove(e);
set.add(anotherE);
}
}
其中 e
和 anotherE
通过提供的比较器相等。
最佳答案
是的,它很安全。来自 java docs :
Insertion, removal, and access operations safely execute concurrently by multiple threads. Iterators are weakly consistent, returning elements reflecting the state of the set at some point at or since the creation of the iterator. They do not throw ConcurrentModificationException, and may proceed concurrently with other operations.
关于java - 迭代时向 ConcurrentSkipListSet 删除和添加元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11508852/