-1

Hi guys I have a small problem. I'm writing a app for android and I have a part of my code like this.

public void loop(){

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

}

I got the error java.util.ConcurrentModificationException I also tried using iterators in order to solve the problem quickly. Also, I use synchronized since I checked the error in oracle docs and it says that could be caused by many threads using the same code.

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

I don't really know why any of the solutions I found didn't work. I use this portion of code most of the cases in a background service and I use it's class that contains that method (using singleton) in the acivity too. Thanks in advance to all the comunity


Thanks for the support guys, -carList is a ArrayList of Car objects. my car class is like this

public class Car{
   private int mSpeed;
   private int mDistance; //zero at first
    ........
   public void run(){
       mSpeed=getRandomSpeed();
       mDistance+=mSpeed;
       updateDistanceInDB();//here I sabe the distance in DB using a DB helper
   }
   .........   

}

-The error indicates that it's generated in the line that executes the method run There's something strange because the app works well in another device I have.

  • can we see the code for `Car#run()`? You should also tell us what `carList` is –  Oct 27 '16 at 05:33
  • Is `carList` accessible from other threads? Or does `car.run()` happen to modify the `carList` ever? – 4castle Oct 27 '16 at 05:33
  • 1
    java.util.ConcurrentModificationException comes when your trying to modify a collection object and looping through it at the same time. you may have to copy carList to other list before looping through it. – WritingForAnroid Oct 27 '16 at 05:35
  • check it [here](http://stackoverflow.com/questions/5151956/java-util-concurrentmodificationexception-in-android-animation) and [here](http://stackoverflow.com/questions/13533715/java-util-concurrentmodificationexception-android-after-remove-elements-from-arr) – Jordan Oct 27 '16 at 05:36
  • RULE #0: When asking a question about an exception, you MUST post the COMPLETE stack trace and identify the line in your code that caused the exception. – Jim Garrison Oct 27 '16 at 05:39
  • In future, please do not cross-post questions (http://es.stackoverflow.com/questions/30100/error-en-android-java-util-concurrentmodificationexception). For more information, see [here](http://meta.stackexchange.com/q/64068). – Matt Oct 28 '16 at 12:40
  • Sorry , Is this a rule? since I really needed help and I speak 2 languages. – Carlos Chávez Oct 28 '16 at 15:25

1 Answers1

2

The most brute-force way to fix this exception is to use a CopyOnWriteArrayList in place of your current List. This will ensure that any updates to the list during a for-each loop won't be seen during iteration, and therefore won't throw a ConcurrentModificationException.

According to its documentation:

The "snapshot" style iterator method uses a reference to the state of the array at the point that the iterator was created. This array never changes during the lifetime of the iterator, so interference is impossible and the iterator is guaranteed not to throw ConcurrentModificationException.

However, there may be a better way to solve your issue depending on the situation. Look for what parts of your code make modifications to the List, and ensure that they don't run during iteration.

4castle
  • 32,613
  • 11
  • 69
  • 106