[Kaffe] I am getting a ConcurrentModificationException

Chris Gray chris at kiffer.eunet.be
Mon Sep 27 14:20:37 PDT 1999


On Mon, 27 Sep 1999, Moses DeJong wrote:

> 
> > However, this exception indicates that your program potentially
> > has a bug.. it's trying to iterate through the elements while
> > changing the contents of the Hashtable at the same time.
> >
> > -Archie
> 
> I wrote a little test program to double check that what I was doing
> was correct. I was using the Hashtable.keys() method to iterate a
> table and remove each key like the following example.
> 
> [...]      
> 	for (search = h.keys(); search.hasMoreElements() ; ) {
> 	    elem = (String) search.nextElement();
> [...]
> 	    h.remove(elem);
> 	    System.out.println("removed");
> 	}

According to my reading of the specs, this code does not necessarily work:
an enumeration is not a snapshot, and deleting stuff as you iterate
through the enumeration is a bit like sawing off the branch you are
sitting on.  Some implementations of keys() would fail in such
circumstances - especially if resizing of the hashtable occurred as a
result of all those removals - and still be correct implemetations.
Someone please correct me if I'm wrong here.

 -- 

  Chris Gray            chris at kiffer.eunet.be    chris_gray at bcs.org.uk


More information about the kaffe mailing list