-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.

7
  • can we see the code for Car#run()? You should also tell us what carList is Commented Oct 27, 2016 at 5:33
  • Is carList accessible from other threads? Or does car.run() happen to modify the carList ever? Commented Oct 27, 2016 at 5: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. Commented Oct 27, 2016 at 5:35
  • check it here and here Commented Oct 27, 2016 at 5: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. Commented Oct 27, 2016 at 5:39

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.