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确实不是支持并发多线程访问。

相关问题