簡體   English   中英

迭代時刪除和添加元素到ConcurrentSkipListSet

[英]Removing and adding elements to ConcurrentSkipListSet while iterating

在迭代它時,刪除並向ConcurrentSkipListSet添加元素是否安全:

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

其中eanotherE相等,提供比較器。

是的,這是安全的。 來自java文檔

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM