Error in Android java.helpful.ConcurrentModificationException

I'm making a app for Android , and some of my code is like this:

public void loop(){

   for(Car car:carList)
        car.run();

}

The bad thing is that I get the error java.util.ConcurrentModificationException. I've been researching, used iterators and Java synchronized notation, because as I read in the Oracle documentation, the error can be generated by multiple threads using my "loop"method.

public synchronized void loop(){
 Iterator<Car> carIterator = carList.iterator();
 while(cardIterator.hasNext()){
  carIterator.next().run();
 }
}

I really don't know what else can fix this problem. I use the class of this method and this same, most of all, in services background and in activities, I also use them (using the pattern singleton to instantiate) .


Thanks guys for the support! Well, to give more details of my problem, the carList is an ArrayList with objects car My car class is as follows:

public class Car{
   private int mSpeed;
   private int mDistance; //inicializo con cero
    ........
   public void run(){
       mSpeed=getRandomSpeed();
       mDistance+=mSpeed;
       updateDistanceInDB();//Aquí lo guardo en una base de dato sqlite
   }
   .........   

}

- the error tells me that this was generated on the line where I run the method run() inside the loop. In addition, what I Strange is that this error does not happen on another cell phone that I have. - This is the the official oracle documentation page that tells me the error may be due to multiple threads using my list, so use synchronized but it didn't fix

Https://docs.oracle.com/javase/7/docs/api/java/util/ConcurrentModificationException.html

Why on other mobiles will it work and others won't? Thank you guys for your help!

 3
Author: Carlos Chávez, 2016-10-27

2 answers

Because as I read in the Oracle documentation, the error can be generated by multiple threads using my "loop" method.

Is not correct. What the exception says is that:

  1. You've created an iterator (well, the for syntax does it for you)

  2. Between call and call to the iterator next(), the content of the original Collection has been modified (basically, someone has called add(), remove() or reset()). Really, it doesn't matter let it be from another thread or from the same (are you sure the run() method doesn't modify the list?). When making the next call to next(), it jumps the exception.

Possible solutions:

1) Identify what other code is modifying the collection, and put your loop and the other code in a critical Section (synchronized), using the same object as lock.

Note that if you are modifying the list from the same thread (for example from the run() method), the synchronization does not help (synchronization prevents two threads from executing blocks of code that interfere with each other at once; if it is the fault of the run() you will be inside the same thread).

2) If you don't want/can with 1), Make a new Collection with the contents of the original and iterate over it; List<Car> listCopy = new ArrayList<>(carList);.

The main problem with this approach is that you don't know what the original cause of the problem is, so you don't know if you would have to update the list because the original list has changed.

 4
Author: SJuan76, 2016-10-27 07:34:56

To fix the error:

Java.helpful.ConcurrentModificationException

What you say "the error can be generated by multiple threads using my "loop" method." It's partly true but what actually happens is that your list is being modified and at the same time it's probably being used in another thread

You can solve this problem by simply creating a copy of your List in this case carList:

List<Car> copiaList = new ArrayList<Car>(carList);  

In this copy you can perform the operations without problem and you will avoid the ConcurrentModificationException error.

 0
Author: Jorgesys, 2017-10-04 16:41:48