0

全部,

遇到 ConcurrentModificationException 问题并努力寻找解决方案,部分原因是我在迭代列表时看不到我在哪里修改列表......有什么想法吗?我已经突出显示了导致问题的行 (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;
                }
            }
        }
    }
}
4

2 回答 2

1

plotDeviceDetails.mListOfPlotChannelDetailsfirstMap.get(mapKey).mListOfPlotChannelDetails引用相同的列表。

如果没有更多信息,是否plotDeviceDetailsfirstMap.get(mapKey)引用同一个对象是未知的,但它们共享频道列表。

于 2015-09-25T21:58:21.347 回答
1

堆栈跟踪显示这mListOfPlotChannelDetails是一个,并且由于堆栈ArrayList跟踪还显示错误来自被 迭代。it3.remove()ArrayListit3

记住,ArrayList支持并发多线程访问。

于 2015-09-26T00:09:38.457 回答