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

concurrent modification not detected on 2nd to last iteration

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: P4
    • Resolution: Won't Fix
    • Affects Version/s: 1.3.0, 1.4.2
    • Fix Version/s: None
    • Component/s: core-libs
    • Labels:

      Description



      Name: gm110360 Date: 08/06/2003


      FULL PRODUCT VERSION :
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.1_01-b01)
      Java HotSpot(TM) Client VM (build 1.4.1_01-b01, mixed mode)

      FULL OPERATING SYSTEM VERSION :
      Microsoft Windows 2000 [Version 5.00.2195]

      A DESCRIPTION OF THE PROBLEM :
      Concurrent modification exception is not thrown on the
      second to last iteration of an iterator.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1. Compile and run the small test program -- it should
         die with concurrent modification exception. If the
         item removed from the list is anything but "c" it works.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      Expected a concurrent modification exception in this case:
      Iterator iter = list.iterator();
      while(iter.next()) {
         Object o = iter.next();
         if(2nd to last item in list) {
            list.remove(o); // <-- expected exception here!
         }
      }

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      the problem is the lack of an exception

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.util.Iterator;
      import java.util.ArrayList;
      import java.util.List;
      import java.util.Vector;

      /**
       * @author Steven R Brandt
       */
      public class CopyIterator {
          public void remove() {
              throw new Error("cannot remove from CopyIterator");
          }

          public static void main(String[] args) throws Exception {
              list.add(new ToyListener("a"));
              list.add(new ToyListener("b"));
              list.add(new ToyListener("c"));
              list.add(new ToyListener("d"));
              Iterator iter = list.iterator();
              while(iter.hasNext()) {
                  ToyListener toy = (ToyListener)iter.next();
                  toy.event();
                  System.out.println(toy);
              }
          }
          static List list = new Vector();
          public static class ToyListener {
              String name;
              ToyListener(String name) {
                  this.name = name;
              }
              public void event() {
                  // this should cause a concurrent modification exception
                  // if "a", "b", or "d" are used it will
                  if(name.equals("c")) {
                      list.remove(this);
                  }
              }
              public String toString() {
                  return "Toy: "+name;
              }
          }
      }
      ---------- END SOURCE ----------

      CUSTOMER WORKAROUND :
      write your own iterator
      (Incident Review ID: 180003)
      ======================================================================

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              jjb Josh Bloch
              Reporter:
              gmanwanisunw Girish Manwani (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: