Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is throwing ConcurrentModificationException system dependent

I am working on a piece of code with Iterator and getting a ConcurrentModificationException at the line a when I run the program from my IDE on windows--

  LinkedList ll =new LinkedList();
  . . .
  . . . 
  Iterator iter = ll.iterator();
  int i=0;
   while (iter.hasNext()) {
       // GrammarSection agrammarSection = (GrammarSection) iter.next();  //a
       String s1 = (String) iter.next();
        ll.remove(i);
        i++;
   }

This is expected because Im modifying the list while I'm iterating so the fail-fast iterator throws a Concurrentmodification exception. However, when I run this code in unix with apache server, the next method of the iterator does-not throw any exception. So, does the concurrentmodification exception depend on OS level ?

like image 966
user496934 Avatar asked May 19 '11 15:05

user496934


People also ask

Which method of iterator throws ConcurrentModificationException?

This exception occurs when an object is attempted to be modified concurrently without permission. For example, if a Collection is modified while a thread is traversing it using an Iterator , a ConcurrentModificationException is thrown from the Iterator. next() method.

Why HashMap throws ConcurrentModificationException?

The ConcurrentModificationException occurs when an object is tried to be modified concurrently when it is not permissible. This exception usually comes when one is working with Java Collection classes. For Example - It is not permissible for a thread to modify a Collection when some other thread is iterating over it.

Why does iterator remove Do not throw ConcurrentModificationException?

ConcurrentModificationException is not thrown by Iterator. remove() because that is the permitted way to modify an collection while iterating. This is what the javadoc for Iterator says: Removes from the underlying collection the last element returned by this iterator (optional operation).


1 Answers

No, it shouldn't. It should crash anyway.

I suppose it could be different on a different JVM, but according to the official spec, iterators on linked list should be fail-fast.

OS has nothing to do with it.

like image 140
Joeri Hendrickx Avatar answered Oct 26 '22 19:10

Joeri Hendrickx