java.util.ConcurrentModificationException 解决 Android

java.util.ConcurrentModificationException 解决       Android  java

在项目里面 遇到了这个bug :

E/AndroidRuntime(22055): java.util.ConcurrentModificationException

E/AndroidRuntime(22055):      at java.util.ArrayList$ArrayListIterator.next(ArrayList.java:569)

查看了下 这个异常介绍:

An ConcurrentModificationException is thrown when a Collection is modified and an existing iterator on the Collection is used to modify the Collection as well.

ConcurrentModificationException 抛出的条件   大意是: 一个迭代器在迭代集合的时候   集合被修改了

举个通俗的栗子

例如    在迭代  Arraylist 的时候  对Arraylist进行增删操作  就会抛出该异常

分析原因:

集合中 list set 等 都没有实现同步  ,  在多线程中   对集合进行操作时  同步操作都是由外部进行控制

再来看一下 Iterator 的工作原理

An iterator over a sequence of objects, such as a collection.

If a collection has been changed since the iterator was created, methods next and hasNext() may throw a ConcurrentModificationException. It is not possible to guarantee that this mechanism works in all cases of unsynchronized concurrent modification. It should only be used for debugging purposes. Iterators with this behavior are called fail-fast iterators.

Implementing Iterable and returning an Iterator allows your class to be used as a collection with the enhanced for loop.

翻译是

如果集合已经改变自创建迭代器,next和hasNext方法()可能抛出ConcurrentModificationException。这是不可能的,以保证这一机制的工作中不同步并发修改的所有情况。它应该只用于调试的目的。迭代器与这种行为被称为快速失败的迭代器。

按照Iterator的工作原理 应该是在一个独立线程里面完成   且Iterator 执行的时候 迭代的对象必须是不可变的  单向的 顺序的

来看看实际解决方案:

1) 在需要迭代的时候 增加一个 锁

但是比较影响效率

2) 每次前迭代 将集合复制一遍

Arraylist 里面有个toarray()方法

    /**
     * Returns a new array containing all elements contained in this
     * {@code ArrayList}.
     *
     * @return an array of the elements from this {@code ArrayList}
     */
    @Override public Object[] toArray() {
        int s = size;
        Object[] result = new Object[s];
        System.arraycopy(array, 0, result, 0, s);
        return result;
    }

toArray() 方法  会调用   System.arraycopy(array, 0, result, 0, s);  将集合copy一遍

在多线程    当我需要迭代Arraylist的时候   在集合后面增加一个  toArray()  将集合复制一遍 .

这样对原集合进行的修改操作并不会影响到copy后的新集合

每日前进一步

转载请注明  http://blog.csdn.net/aaawqqq/article/details/43884623

多有不足

希望和大家有更多交流

谢谢

时间: 2024-08-01 19:22:48

java.util.ConcurrentModificationException 解决 Android的相关文章

java.util.ConcurrentModificationException 解决办法

在使用iterator.hasNext()操作迭代器的时候,如果此时迭代的对象发生改变,比如插入了新数据,或者有数据被删除. 则使用会报以下异常: java.util.ConcurrentModificationException         at java.util.HashMap$HashIterator.nextEntry(HashMap.java:793)         at java.util.HashMap$KeyIterator.next(HashMap.java:828)

java.util.ConcurrentModificationException 异常解决办法及原理

最近在修程序的bug,发现后台抛出以下异常: Exception in thread "main" java.util.ConcurrentModificationException at java.util.HashMap$HashIterator.nextEntry(HashMap.java:793) at java.util.HashMap$KeyIterator.next(HashMap.java:828) at com.keyman.demo.test.ClearResult

java.util.ConcurrentModificationException的解决办法

今天在使用iterator.hasNext()操作迭代器的时候,当迭代的对象发生改变,比如插入了新数据,或者有数据被删除. 编译器报出了以下异常: Exception in thread "main" java.util.ConcurrentModificationException at java.util.LinkedHashMap$LinkedHashIterator.nextNode(LinkedHashMap.java:719) at java.util.LinkedHash

android详细信息java.util.ConcurrentModificationException变态

在今天做android当项目,我遇到了这个异常,好吧.其实最不寻常遇到异常IllegalstateException.它们与我们的硬件连接SDK抛出,我想折磨学生阿玉啊.扯远了. 今天,我想回到这个异常.java.util.ConcurrentModificationException异常,一開始我愣了一下.貌似从来没遇到过这个,然后果断百度大神.这才发现: 原因是你遍历该集合时.对该集合进行了删除元素的操作导致的.假设你有删除元素的必要,建议赋值到还有一个集合,然后对他进行删除操作. 偶出现错

android细节之java.util.ConcurrentModificationException异常

今天在做android项目的时候,遇到了这个异常,好吧,其实平常遇到最多的异常是IllegalstateException,都是跟我们硬件相连的SDK抛出来的,把我折磨的欲生欲死啊.扯远了.说回到今天的这个异常,java.util.ConcurrentModificationException异常,一开始我愣了一下,貌似从来没遇到过这个,然后果断百度大神,这才发现: 原因是你遍历该集合时,对该集合进行了删除元素的操作导致的,如果你有删除元素的必要,建议赋值到另一个集合,然后对他进行删除操作. 偶

java.util.ConcurrentModificationException异常原因及解决方法

在java语言中,ArrayList是一个很常用的类,在编程中经常要对ArrayList进行删除操作,在使用remove方法对ArrayList进行删除操作时,报java.util.ConcurrentModificationException异常,下面探讨一下该异常的原因以及解决办法. 1 import java.util.ArrayList; 2 import java.util.List; 3 4 public class Test { 5 6 public static void mai

Java.util.ConcurrentModificationException的理解与修改

在Java开发的过程中有没有遇到类似的异常信息 Exception in thread "main" java.util.ConcurrentModificationException, 下面介绍异常原因以及这种异常的改进方法,内容很简单,有什么问题还望指正. 假设我们要实现这样一个例子: 判断集合里面有没有"world"这个元素,如果有,就添加一个"javaee"元素 出现异常的代码如下: import java.util.ArrayList;

遇到java.util.ConcurrentModificationException

今天犯了一个简单的错: List<Book> books = adapter.data; for(Book book: books){     if(book.id == bookId){         books.remove(book);     } } 结果遇到java.util.ConcurrentModificationException,起初以为是Android里操作不在同一线程的UI的问题,其实没那么复杂,只是犯了一个常见的Java错误.改为下面即可: List<Book

小小异常:java.util.ConcurrentModificationException

1 public static void main(String[] args) { 2 ArrayList<String> list = new ArrayList<String>(); 3 for(int i = 0; i < 5; i++){ 4 list.add("str"+i); 5 } 6 for(String item:list){ 7 if("str2".equals(item)){ 8 list.remove(&quo