简体   繁体   English

此代码块的并发修改异常,请帮助?

[英]Concurrent Modification exception for this code block, Help Please?

missedMSRB is a List with 2187 elements in it and when trying to run the below snippet missedMSRB是一个列表,其中包含2187个元素,并且在尝试运行以下代码段时

List<List<String>> subList = getSubList(missedMSRB, 1000);

    for (List<String> subMSRB : subList) {

        StringBuffer sql = new StringBuffer(NamedQueries.msSQL); 
        sql.append("(");

        for (int i1 = 0; i1 < subMSRB.size(); i1++) {  //Line 463 Throws Exception

            if (i1 < subMSRB.size() - 1) {
                sql.append("?,");
            } else {
                sql.append("? )");

            }

        } ....

Code fails with the follwoing exception any suggestions why i am getting concurrent modification and how to get rid of the same 代码失败,出现以下异常,任何有关为什么我要进行并发修改以及如何摆脱相同建议的建议

13 Jan 2015 10:42:58,974 [main] ERROR  RunAnalytics:  General Error: null
java.util.ConcurrentModificationException
    at java.util.ArrayList$SubList.checkForComodification(ArrayList.java:1169)
    at java.util.ArrayList$SubList.size(ArrayList.java:998)
    at com.abc.Analytics.RunAnalytics.getCountCheck(RunAnalytics.java:463)
    at com.abc.Analytics.RunAnalytics.analyticsExecute(RunAnalytics.java:342)
    at com.abc.Analytics.RunAnalytics.main(RunAnalytics.java:84)

Remaining code below 下面的剩余代码

PreparedStatement psMSQL2 = msSQL.prepareStatement(sql.toString());

    psMSQL2.setString(1, runDate);
    psMSQL2.setString(2, runDate2);

    int i = 3;
    for (String s : subMSRB) {
        psMSQL2.setString(i, s.trim());
        i++;
    }

    ResultSet msSQL = psMSQL2.executeQuery();
    logger.debug("SQL executed");

    while (msSQL.next()) {
        missedMSRB.remove(msSQL.getString(1));
    }

getSubList impl// Corrected getSubList impl //已更正

public static List<List<String>> getSubList(List<String> inputList, int subListSize) {

        int listSize = inputList.size();
        int noOfLoops = listSize / subListSize;
        int remainingListSize = listSize % subListSize;

        List<List<String>> subList = new ArrayList<List<String>>();

        for (int i = 0; i < noOfLoops; i++) {
            int fromIndex = i * subListSize;
            int toIndex = (fromIndex) + subListSize;
            subList.add(new ArrayList<String>(inputList.subList(fromIndex, toIndex)));

            if ((remainingListSize != 0)
                    && (toIndex == (listSize - remainingListSize))) {
                subList.add(new ArrayList<String>(inputList.subList(toIndex, listSize)));

            }
        }

        return subList;

    }

In getSubList you create several lists using inputList.subList . getSubList中,使用inputList.subList创建多个列表。 This method from List implemented in ArrayList does not create a new List. ArrayList中实现的List此方法不会创建新的List。 Instead, it returns a view of inputList , and is therefore backed by inputList . 相反,它返回inputList的视图,因此由inputList支持。

Any modification to inputList has an impact on the sublist. inputList任何修改inputList列表产生影响。 You have passed missedMSRB in as inputList , so any modification to missedMSRB will impact on subList 您已将missedMSRB作为inputList传递了,因此对missedMSRB任何修改都会影响subList

Therefore, when you call missedMSRB.remove within the outer for loop, you create a ConcurrentModification to all the lists in subList . 因此,当你调用missedMSRB.remove外内for循环,你创建一个ConcurrentModification在所有列表subList When you then check the size of one of this lists in subList it throws the ConcurrentModificationException 然后,当您检查subList中此列表之一的大小时,它将引发ConcurrentModificationException

To solve this, you can for example create a new list instead of using directly the result of subList : 为了解决这个问题,您可以例如创建一个新列表,而不是直接使用subList的结果:

subList.add(new ArrayList<String>(inputList.subList(fromIndex, toIndex)));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM