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

ArrayList updates modCount on sorting, which break compatibility

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: P3
    • Resolution: Not an Issue
    • Affects Version/s: 8u45, 9
    • Fix Version/s: 9
    • Component/s: core-libs
    • Labels:

      Description

      FULL PRODUCT VERSION :
      java version "1.8.0_45"
      Java(TM) SE Runtime Environment (build 1.8.0_45-b14)
      Java HotSpot(TM) 64-Bit Server VM (build 25.45-b02, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows [Version 6.1.7601]

      A DESCRIPTION OF THE PROBLEM :
      ArrayList class updates the modCount causing ConcurrentUpdateException to appear where it did not appear in previous versions.
      This seems to be logically inconsistent, as sorting is just a series of calls to set() method.

      Developer can sort List manually by directly calling get and set methods this wouldn increase mod count.

      To make the situation consistent one should either update modCount in set() method as well, or do not update it in sort() method

      REGRESSION. Last worked in version 7u75

      ADDITIONAL REGRESSION INFORMATION:
      java version "1.7.0_21"
      Java(TM) SE Runtime Environment (build 1.7.0_21-b11)
      Java HotSpot(TM) 64-Bit Server VM (build 23.21-b01, mixed mode)

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run code below.


      public class SubListIssue {

          public static void main(String[] args) {
              List<Double> fullList = new ArrayList<Double>(Arrays.asList(3., 2., 0., 1., 2.));
              
              List<Double> subList = fullList.subList(0, 3);
              
              Collections.sort(fullList);
              
              System.out.println(subList.get(0));

          }
      }

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Access to sublist after sorting does not cause exception. Application writes "0.0"
      ACTUAL -
      Exception is thrown

      Exception in thread "main" java.util.ConcurrentModificationException
      at java.util.ArrayList$SubList.checkForComodification(ArrayList.java:1231)
      at java.util.ArrayList$SubList.get(ArrayList.java:1035)

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      public class SubListIssue {

          public static void main(String[] args) {
              List<Double> fullList = new ArrayList<Double>(Arrays.asList(3., 2., 0., 1., 2.));
              
              List<Double> subList = fullList.subList(0, 3);
              
              Collections.sort(fullList);
              
              System.out.println(subList.get(0));

          }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Do not use subList. Create own implementation,

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              igerasim Ivan Gerasimov
              Reporter:
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              5 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: