ConcurrentModificationException

Discussions

Web tier: servlets, JSP, Web frameworks: ConcurrentModificationException

  1. ConcurrentModificationException (2 messages)

    I always get the same Exception when i try to execute a normal recursive code as follow:

    public List getCanalesUsuario(Channel channel,boolean excludeParent) throws Exception
    {
     ArrayList list = new ArrayList();

     if (!excludeParent) list.add(channel);
     Channel[] subcanales = channel.getAllSubchannels();
     for (int i = 0; i < subcanales.length; i++) {
       list.addAll(getCanalesUsuario(subcanales[i], false));
     }
     return list;
    }

    The complet exception is:

    "java.util.ConcurrentModificationException at java.util.AbstractList$Itr.checkForComodification(AbstractList.java(Inlined Compiled Code))"

    I have already tryed with a Vector instead of a List because it's synchronized, but it doesn't work too. Does anyone know anything about it?
    Thanks in advance,
    Álvaro

    Threaded Messages (2)

  2. iterators and mods[ Go to top ]

    You are probably using an iterator in the "getAllSubchannels()" method call. Iterators will fail if the collection is "changed". You should probably change your code so that the data structure that gets modified is not the one you iterator through, i.e. iterator and build up a second collection.

    Bruce
  3. iterators and mods[ Go to top ]

    Thanks!!!

    You are right but i have found a easier solution: just adding synchronized and using vector instead of ArrayList(it's unsynchronized).

     It works fine ;)