欢迎光临
一个有态度、有温度的分享型博客

java.util.ConcurrentModificationException解决办法

今天在遍历list集合时同时对list集合进行增加和删除操作,导致了以下错误:

java.util.ConcurrentModificationException
    at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:819)
    at java.util.ArrayList$Itr.next(ArrayList.java:791)
    at com.cdsoft.epcp.android.InstanceManagerAndroidController$1.run(InstanceManagerAndroidController.java:546)

代码如下:

List<String> endTimeList = new ArrayList<>();
                                List<OperateCameraInfo> withSameIpCameraInfos = new ArrayList<>();
                                for (int i = 0; i < operateCameraInfoList.size(); i++) {
                                    if (i == 0) {
                                        withSameIpCameraInfos.add(operateCameraInfoList.get(i));
                                    }
                                    if (i  != 0 && i == (operateCameraInfoList.size() - 1)) {
                                        withSameIpCameraInfos.add(operateCameraInfoList.get(i));
                                        endTimeList.add(audioCameraInfos.get(audioCameraInfos.size() - 1).getEND_TIME());
                                        groupHash.put(audioCameraInfos.get(audioCameraInfos.size() - 1).getEND_TIME(), withSameIpCameraInfos);
                                    }
                                    if (i != 0 && i != (operateCameraInfoList.size() - 1)) {
                                        String curOpCameraIp = operateCameraInfoList.get(i).getCAMERA_IP();
                                        String preOpCameraIp = operateCameraInfoList.get(i - 1).getCAMERA_IP();
                                        if (StringUtils.isNotBlank(curOpCameraIp) && StringUtils.isNotBlank(preOpCameraIp)) {
                                            if (curOpCameraIp.equals(preOpCameraIp)) {
                                                withSameIpCameraInfos.add(operateCameraInfoList.get(i));
                                            } else {
                                                endTimeList.add(withSameIpCameraInfos.get(withSameIpCameraInfos.size() - 1).getEND_TIME());
                                                groupHash.put(withSameIpCameraInfos.get(withSameIpCameraInfos.size() - 1).getEND_TIME(), withSameIpCameraInfos);
                                                withSameIpCameraInfos = new ArrayList<>();
                                                withSameIpCameraInfos.add(operateCameraInfoList.get(i));
                                            }
                                        }
                                    }
                                }

其中这段代码实在一个Thread线程里操作的,上面的代码我对endTimeList集合进行操作所以报了这个错误,解决的方法是我使用了CopyOnWriteArrayList如下:

List<String> endTimeList = new CopyOnWriteArrayList<>();

参考以下文章:

  1. http://www.cnblogs.com/dolphin0520/p/3933551.html;
  2. http://www.jianshu.com/p/c5b52927a61a;
  3. http://blog.csdn.net/mazhimazh/article/details/19210547;

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址