List<Ball> myObjs = myThreads[threadIndex].getMyObjList();
int initialSize = Collections.synchronizedList(myObjs).size();
Throws ConcurrentModificationException. I have also tried putting this in a
synchronized(myObjs) block, but it also didn’t work. What is the solution? Everywhere else where I use this list, it is in an synchronized(block).
P.S. This error also ends up producing BrokenBarrierException. (yes, I am using cyclic barrier for synchronization)
EDIT: Here is the stack trace:
Exception in thread "Thread-3" java.util.ConcurrentModificationException
at java.util.ArrayList$SubList.checkForComodification(ArrayList.java:1091)
at java.util.ArrayList$SubList.size(ArrayList.java:921)
at java.util.Collections$SynchronizedCollection.size(Collections.java:1573)
at Part2.Animation.processCollisions(MyClass.java:133) // This is the call to size()
EDIT: The loop looks like
for (int threadIndex=0; threadIndex < numThreads; threadIndex++) {
List<Ball> myObjs = myThreads[threadIndex].getMyObjList();
int initialSize = Collections.synchronizedList(myObjs).size();
}
And regardless of numThreads, the exception happens when threadIndex=1.
Looking at the source code for some of the
Listclasses, I can’t see how it is possible that callingsize()will throw aConcurrentModificationException. It would help if you showed us a stack trace.But in the meantime, you appear to have a fundamental misconception about what
Collections.synchronizedListdoes. What it does is create and return a list wrapper that will ensure that operations on the same wrapper instance are synchronized.It does not in anyway prevent threads from performing unsynchronized operations on underlying list; i.e. the list object that you just wrapped. If a thread has the reference for the underlying list, it can access it directly without going thrpough the wrapper. And neither does it do anything to synchronize operations using different wrappers for the same underlying list. So if you call
Collections.synchronizedListtwice on the same list, you will create two distinct wrappers that do not synchronize with each other.So, in fact your
Collections.synchronizedList(myObjs).size()does not perform any meaningful synchronization at all. No other thread can get hold of the synchronized list wrapper that is created, so no other thread can synchronize with this one via the wrapper.