Java.util.ConcurrentModificationException solving Android

Source: Internet
Author: User
Tags concurrentmodificationexception

Java.util.ConcurrentModificationException solving Android java


This bug was encountered in the project:

E/androidruntime (22055): Java.util.ConcurrentModificationExceptionE/androidruntime (22055): at Java.util.arraylist$arraylistiterator.next (arraylist.java:569)

Check out the following exception description:
An concurrentmodificationexception was thrown when a Collection was modified and an existing iterator on the Collection are U Sed to modify the Collection as well.


Concurrentmodificationexception throws a condition to the effect that an iterator is modified at the time the collection is iterated.
to give a popular chestnutFor example, when iterating over the Arraylist, the exception is thrown when the Arraylist is deleted.

Analysis Reason:The list set in the collection is not synchronized, and the synchronous operation is externally controlled when the collection is manipulated in multiple threads .
and look at how Iterator works.
An iterator over a sequence of objects, such as a collection. If A collection has been changed since the iterator is created, methods Next and Hasnext () may throw a Concurrentmodifica Tionexception. It is a possible to guarantee the this mechanism works in all cases of unsynchronized concurrent modification. It should only is used for debugging purposes. Iterators with this behavior is called Fail-fast iterators. Implementing Iterable and returning an Iterator allows your class to is used as a collection with the enhanced for loop.

translation isIf the collection has changed since the creation of the iterator, the next and Hasnext methods () may throw concurrentmodificationexception. It is not possible to ensure that this mechanism works without synchronizing concurrent modifications in all cases. It should only be used for debugging purposes. Iterators with this behavior are called fast-failed iterators.

In accordance with the principle that iterator should be done in a separate thread and iterator execution, the iterated object must be in an immutable one-way order.

to see the actual solution:
1) Add a lock when iteration is requiredBut the comparison affects the efficiency

2) Copy the collection once per previous iteration
Arraylist There is a ToArray () method
    /**     * 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;    }

The ToArray () method invokes System.arraycopy (array, 0, result, 0, s); Copy the collection again
in multithreading when I need to iterate ArrayList, add a ToArray () after the collection to copy the collection again.This modification to the original collection does not affect the new collection after the copy

Daily before further
reprint Please specify http://blog.csdn.net/aaawqqq/article/details/43884623
There are many deficiencieshope to have more communication with youThanks



Java.util.ConcurrentModificationException solving Android

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.