Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6570631

CopyOnWriteArrayList subLists prohibit even non-structural modifications to parent list

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • 7
    • 7
    • core-libs
    • None

        The spec for CopyOnWriteArrayList.subList(...) strongly suggests that
        non-structural modifications to parent list (e.g. set(int, Object))
        are permitted while a subList is active. In fact, this leads to
        ConcurrentModificationException.
         
            * Returns a view of the portion of this list between
             * <tt>fromIndex</tt>, inclusive, and <tt>toIndex</tt>, exclusive.
             * The returned list is backed by this list, so changes in the
             * returned list are reflected in this list, and vice-versa.
             * While mutative operations are supported, they are probably not
             * very useful for CopyOnWriteArrayLists.
             *
             * <p>The semantics of the list returned by this method become
             * undefined if the backing list (i.e., this list) is
             * <i>structurally modified</i> in any way other than via the
             * returned list. (Structural modifications are those that change
             * the size of the list, or otherwise perturb it in such a fashion
             * that iterations in progress may yield incorrect results.)

        public class Bug35 {
            public static void main(String[] args) throws Throwable {
        java.util.List x
        = new java.util.concurrent.CopyOnWriteArrayList();
        x.add("foo");
        java.util.List y = x.subList(0,1);
        x.set(0, "bar");
        y.get(0);
            }
        }

        =>
        Exception in thread "main" java.util.ConcurrentModificationException
        at java.util.concurrent.CopyOnWriteArrayList$COWSubList.checkForComodification(CopyOnWriteArrayList.java:1121)
        at java.util.concurrent.CopyOnWriteArrayList$COWSubList.get(CopyOnWriteArrayList.java:1150)
        at Bug35.main(Bug35.java:8)

              martin Martin Buchholz
              martin Martin Buchholz
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: