2012-07-16 48 views
0

它是安全的刪除和添加元素ConcurrentSkipListSet在遍歷它:刪除和添加元素ConcurrentSkipListSet而迭代

ConcurrentSkipListSet<Element> set = new ConcurrentSkipListSet<Element>(myComparator); 
for(Element e : set) 
{ 
    if(condition) 
    { 
     set.remove(e); 
     set.add(anotherE); 
    } 
} 

其中eanotherE是提供比較平等的。

回答

2

是的,它是安全的。從java docs

插入,刪除和訪問操作安全由多個線程併發執行 。迭代器是弱一致的,返回元素反映在創建迭代器時或自從創建迭代器以來某個時刻的集合狀態。他們不會拋出 ConcurrentModificationException,並且可能會與其他操作同時進行 。