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.