0

所有,迭代器,但仍ConcurrentModificationException的

運行到ConcurrentModificationException的問題,並努力尋找解決部分是因爲我不能看到我modifiying列表而迭代它...任何想法?我突出顯示了導致問題的一行(it3.remove())。真正在這一個停頓..

編輯:堆棧跟蹤:

Exception in thread "Thread-4" java.util.ConcurrentModificationException 
    at java.util.ArrayList$Itr.checkForComodification(Unknown Source) 
    at java.util.ArrayList$Itr.next(Unknown Source) 
    at com.shimmerresearch.advancepc.InternalFrameAndPlotManager.subtractMaps(InternalFrameAndPlotManager.java:1621) 

線1621對應於it3.remove()在我上面提到的代碼。

private void subtractMaps(ConcurrentSkipListMap<String, PlotDeviceDetails> firstMap, ConcurrentSkipListMap<String, PlotDeviceDetails> secondMap) { 

    // iterate through the secondmap 
    Iterator<Entry<String, PlotDeviceDetails>> it1 = secondMap.entrySet().iterator(); 
    while (it1.hasNext()) { 
     Entry<String, PlotDeviceDetails> itEntry = (Entry) it1.next(); 
     String mapKey = (String) it1Entry.getKey(); 
     PlotDeviceDetails plotDeviceDetails = (PlotDeviceDetails)it1Entry.getValue(); 

     // if we find an entry that exists in the second map and not in the first map continue 
     if(!firstMap.containsKey(mapKey)){ 
      continue; 
     } 

     // iterate through a list of channels belonging to the secondmap 
     Iterator <PlotChannelDetails> it2 = plotDeviceDetails.mListOfPlotChannelDetails.iterator(); 
     while (it2.hasNext()) { 
      PlotChannelDetails it2Entry = it2.next(); 

      // iterate through a list of channels belonging to the firstmap 
      Iterator <PlotChannelDetails> it3 = firstMap.get(mapKey).mListOfPlotChannelDetails.iterator(); 
      innerloop: 
      while(it3.hasNext()){ 
       // if a channel is common to both first map and second map, remove it from firstmap 
       PlotChannelDetails it3Entry = it3.next(); 
       if(it3Entry.mChannelDetails.mObjectClusterName.equals(it2Entry.mChannelDetails.mObjectClusterName)){ 
        it3.remove(); // this line is causing a concurrentModificationException 
        break innerloop; 
       } 
      } 
     } 
    } 
} 
+0

請將完整的堆棧跟蹤添加到您的問題。堆棧跟蹤還包含發生問題的確切行號,所以它可以幫助您突出顯示它,而不用猜測。 – RealSkeptic

+0

首先使用適當的泛型讓您的代碼更易於閱讀。 – chrylis

+0

@RealSkeptic堆棧跟蹤在編輯中添加..它確認引發異常的行是it3.remove() – Strokes

回答

1

plotDeviceDetails.mListOfPlotChannelDetailsfirstMap.get(mapKey).mListOfPlotChannelDetails參考相同名單。

是否plotDeviceDetailsfirstMap.get(mapKey)也引用相同的對象是未知的,沒有更多的信息,但他們共享頻道列表。

+0

它們是名稱相同的列表only..plotDeviceDetails.mListOfPlotChannelDetails是secondmap的列表條目,其中firstMap.get( mapKey).mListOfPlotChannelDetails是firstmap的列表條目。他們不參考同一個對象, – Strokes

1

堆棧跟蹤顯示mListOfPlotChannelDetailsArrayList,由於堆棧跟蹤還顯示錯誤來自it3.remove(),並且代碼中沒有任何東西可以導致該錯誤,所以您確實正在面臨併發修改,即另一個線程更新了it3迭代的ArrayList

請記住,ArrayList確實不是支持併發多線程訪問。

相關問題