I have Android multi-threading application.
There is some probability that two or more triggers might run the same part of code.
I have a list of objects.
I made it to be synchronized by Collections.synchronizedList
private List<WmGroupItemSample> mGroupItemSampleList;
mGroupItemSampleList = new ArrayList<WmGroupItemSample>();
mGroupItemSampleList = Collections.synchronizedList(mGroupItemSampleList);
However sometimes I get Exception on line:
Collections.sort(mGroupItemSampleList, new GroupItemSampleComparator());
java.util.ConcurrentModificationException
at java.util.AbstractList$SimpleListIterator.next(AbstractList.java:62)
at java.util.Collections.sort(Collections.java:1895)
- Is this flow legal?
- do I need create the copy and run sort on copy?
- why
Collections.synchronizedList
doesn't prevent this Exception?
[EDIT]
GroupItemSampleComparator
public class GroupItemSampleComparator implements java.util.Comparator<WmGroupItemSample> {
public GroupItemSampleComparator() {
super();
}
public int compare(WmGroupItemSample s1, WmGroupItemSample s2) {
return ( (s2.getStartDate() - s1.getStartDate()) > 0 ) ? (-1) : (1);
}
}
Thanks,