1

I have a list my defined object that return from a network service ( network svc will update this list). At a certain point of time, I want to render what I have already had in that this on my listview. I have following code:

List<MyObject> myObjects = MyService.getInstance().getListOfMyObject()

And because I have another thread which will updated myObjects list, so I want to avoid concurrency modification exception. I have:

 List<MyObject> clonedList = null;
 synchronized(myObjects){
         clonedList = MyListUtils.cloneList(myObjects)
 }

cloneList() function in MyListUtils is implemented in a simple way:

public static <T extends ICloneable<T>> List<T> cloneList(final List<T> source){
    List<T> result  = new ArrayList<T>();
    for(T t : source){
       T newObject = t.clone();
       result.add(newObject);
    }
    return result;
}

-Dont care about ICloneable interface, I use this ICloneable to mark thing.

And I was given this exception

03-11 16:07:11.370: E/AndroidRuntime(8076): FATAL EXCEPTION: main
03-11 16:07:11.370: E/AndroidRuntime(8076): java.util.ConcurrentModificationException
03-11 16:07:11.370: E/AndroidRuntime(8076):  at java.util.ArrayList$ArrayListIterator.next(ArrayList.java:569)
03-11 16:07:11.370: E/AndroidRuntime(8076):  at xxx.utilities.MyListUtils.cloneList(CollectionUtils.java:120)

I don't know what's wrong with it? I did not call any function to modify object,I just "clone" it, as far as I know this doesn't change any single bit in my object. Any idea is appreciated, thanks.

toantran
  • 1,789
  • 17
  • 25

1 Answers1

1

As per some thread I was able to find like java.util.ConcurrentModificationException in Android animation and How to handle ConcurrentModificationException in Android

If you are iterating over the list and at the same time some other thread is also modyfing it.. this would also cause the ConcurrentModificationException.. So you might try with sychronizing the cloneList method on the passed object of the list..

synchronized(source)
{
    // make a copy
}

I am hoping that you are already doing this in the thread that is modifying the data...

Community
  • 1
  • 1
Praful Bhatnagar
  • 7,425
  • 2
  • 36
  • 44
  • yes, I did it ( as you can see in my editted post), I have put my source inside a synchronized block. But it still throws exception. – toantran Mar 11 '13 at 09:25
  • can you also post the code that is modifying the data... are you synchronizing that part also... – Praful Bhatnagar Mar 11 '13 at 09:30
  • MyService.getInstance().updateListOfMyObject() --> this function is where I modify data ( I upate new data from network connection). I have a list of object to cache my objects there, for updating new object and to retrieve – toantran Mar 11 '13 at 09:31
  • can you please post the relevant code from `updateListOfMyObject()` method? – Praful Bhatnagar Mar 11 '13 at 09:32
  • I just read data from network connection, then rebuild into object , then update these new object into the list cached in MyService. – toantran Mar 11 '13 at 09:38
  • so when you update the list are you using the `synchronized(cached_list_object)` for the code that updates the list with the new objects.. – Praful Bhatnagar Mar 11 '13 at 10:19
  • yes, I do use that sync keyword with my cached_list_object in MyService – toantran Mar 11 '13 at 10:27
  • I think I found it, even though I use synchronized keyword in my update function, but in my return function (in MyService) , I miss that. After add it to return function than things get into right track now :) Thanks for help @Praful Bhatnagar – toantran Mar 11 '13 at 10:41