Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ConcurrentModificationException fix problem

I have a problem. Accessing to a Vector concurrently I get an ConcurrentModificationException. I added synchronized blocks to all the vector iterations but maybe forgot one or there is another leak.

The problem is that the in error stacktrace the error shown is a call to Vector.retainAll() which is a synchronized method. How can I guess whats the other point involved in the collision?

Thanks in advance

 08-24 13:37:25.968: ERROR/AndroidRuntime(6582): java.util.ConcurrentModificationException
 08-24 13:37:25.968: ERROR/AndroidRuntime(6582):     at java.util.AbstractList$SubAbstractList.listIterator(AbstractList.java:320)
 08-24 13:37:25.968: ERROR/AndroidRuntime(6582):     at java.util.AbstractList$SubAbstractList.iterator(AbstractList.java:307)
 08-24 13:37:25.968: ERROR/AndroidRuntime(6582):     at java.util.AbstractCollection.contains(AbstractCollection.java:128)
 08-24 13:37:25.968: ERROR/AndroidRuntime(6582):     at java.util.Collections$SynchronizedCollection.contains(Collections.java:432)
 08-24 13:37:25.968: ERROR/AndroidRuntime(6582):     at java.util.AbstractCollection.retainAll(AbstractCollection.java:319)
 08-24 13:37:25.968: ERROR/AndroidRuntime(6582):     at java.util.Vector.retainAll(Vector.java:856)
like image 264
Addev Avatar asked Apr 02 '26 13:04

Addev


1 Answers

Check for a code where you structurally modify Vector (add or remove) while iterating it - this is most likely reason why you're getting CME. Use iterator to do such a modification to avoid CME

void unsafeMod(Vector data) {
    for (Object o : data) {
        if (o != null) {
            data.remove(o); // may cause CME
        }
    }
}

Also I second an advice to use ArrayList instead of Vector.

like image 134
gnat Avatar answered Apr 04 '26 04:04

gnat