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

ConcurrentModificationException when sorting ArrayList sublists

XMLWordPrintable

      After JDK-8325679 (Optimize ArrayList subList sort), sequentially creating two sublists from an arraylist and then sorting both of them fails with a CME.

      The review for JDK-8325679 mentions 'this doesn't change observable behaviour'. Since it appears to have changed observable behaviour, could this be backed out, and re-done with a CSR if the behaviour change ends up being necessary?

      import java.util.ArrayList;
      import java.util.List;
      import java.util.Collections;
      class T {
        public static void main(String[] args) {
          List<Integer> l = new ArrayList<>(List.of(1, 2, 3, 4));
          var a = l.subList(0, 2);
          var b = l.subList(2, 4);
          Collections.sort(a);
          Collections.sort(b);
          System.err.println(l);
        }
      }

      With openjdk full version "24-ea+13-1421"

      java T.java
      [1, 2, 3, 4]

      With openjdk full version "24-ea+14-1523"

      Exception in thread "main" java.util.ConcurrentModificationException
              at java.base/java.util.ArrayList$SubList.checkForComodification(ArrayList.java:1498)
              at java.base/java.util.ArrayList$SubList.sort(ArrayList.java:1588)
              at java.base/java.util.Collections.sort(Collections.java:145)
              at T.main(T.java:10)

            attila Attila Szegedi
            cushon Liam Miller-Cushon
            Votes:
            0 Vote for this issue
            Watchers:
            7 Start watching this issue

              Created:
              Updated:
              Resolved: