简体   繁体   中英

java ConcurrentModificationException even with synchronized

Similar issue has been posted before but this case is different - there is static usage which may be the complicating it. Just want to see if anyone has ideas on how to handle this. I get the ConcurrentModificationException even though I am using synchronzed on the list around both blocks that modify it.

public class Foo {
   public void register() {
       FooManager.addFoo(this);
   }
}

public class ABC1 {
   static Foo myfoo;
   static {
     myfoo = new Foo();
     myfoo.register();
   }
}

(I have mutliple similar classes ABC2, ABC3)

public class FooManager {
   static ArrayList<Foo> m_globalFoos;
   static ABC1 m_abc;
   static {
     m_globalFoos = new ArrayList<Foo>();
     m_abc = new ABC1();
   }


   public static void addFoo(Foo foo) {
     synchronized(m_globalFoos) { // SYNC
         m_globalFoos.add(foo);
      }
   }

    public static void showFoos() {
        synchronized(m_globalFoos) { //SYNC
            for (Foo foo : m_globalFoos) {
                     foo.print();
            }
    }
}

I declare ABC1, ABC2, ABC3 etc in more than 1 thread func. In my main program, first line

main() {
    FooManager.showFoos();

Exception details:

Exception in thread "main" java.util.ConcurrentModificationException
        at java.util.AbstractList$Itr.checkForComodification(AbstractList.java:372)
        at java.util.AbstractList$Itr.next(AbstractList.java:343)
        at com.mytest.FooManager.showFoos(FooManager.java:78)
        at com.mytest.FooTest.main(FooTest.java:109)

Actually, your intrinsic lock is on the ArrayList that you are iterating. Looks like either the FooHandler OR the print() function has a reference back to your ArrayList which is trying to add/remove content to it. According to JAVADOC, this exception can happen because of either the same thread or a different thread, but not always a different thread. So, if you have some kind of operation that is trying to modify your Arraylist, then this error can occur.

try to use fail-fast iterators for avoiding such errors.

You don't include a lot of code, but my guess is that foo.print is doing something which is ultimately invoking a call to addFoo (pretty much guarantee that the stacktrace for the CME has both showFoos and addFoo in it). 99 times out of 100, ConcurrentModificationException is caused by a single thread, not multiple threads (despite the confusing name).

(and yes, this "bug" is the same as all the other SO posts about CME).

您可能使用其他类,在您的main()中使用FooHandler,但您没有在您的问题中提供其代码...如果不是,请在此处发布异常的堆栈跟踪。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM