CopyOnWriteArrayList throwing CurrentModificationException CopyOnWriteArrayList throwing CurrentModificationException multithreading multithreading

CopyOnWriteArrayList throwing CurrentModificationException


CopyOnWriteArrayList.subLists throw ConcurrentModificationExceptions if the containing list changes out from underneath it:

public class ListTest {  private static List<int[]> intList;  public static void main (String[] args) {    CopyOnWriteArrayList<Integer> cowal = new CopyOnWriteArrayList<Integer>();    cowal.add(1);    cowal.add(2);    cowal.add(3);    List<Integer> sub = cowal.subList(1, 2);    cowal.add(4);    sub.get(0); //throws ConcurrentModificationException  }}


Sbodd has the correct answer, but it sounds like using CopyOnWriteArrayList instead of ArrayList is just an attempt to mask the error. The true problem is an attempt to modify the underlying list while iterating over it. You need to find where in your code you are accessing it as such and remove that usage or work around it.