• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Iterator are fail safe while Enumeration is not

 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Iterator is a thread safe while enumeration is not .

By this i understand that

when we are iterating over a vector a ConcurrentModificationException can be thrown if an another thread modifies the content of the vector.

But this will not happen in the case of a Enumeration.

Am i correct in understanding this ??
 
Campbell Ritchie
Sheriff
Pie
Posts: 49367
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I never knew iterators were thread safe; are they really? Please check your question.
 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry i mean to ask is Iterators are Fail safe .
 
Rob Spoor
Sheriff
Pie
Posts: 20546
56
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Enumerations can also throw a ConcurrentModificationException if the Enumeration is written to do so.

In this case, the Vector iterators are fail-fast, not thread-safe, and the Enumeration returned by elements() is neither.
 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for clarifying.
 
Abhineet Kapil
Ranch Hand
Posts: 52
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,


Iterator : It is fail-fast. That is it fails immediately if an attempt is made to modify a collection while iterating over it.
Although modification (remove elements) can be made in the collection through Iterators remove method only.
Enumerator : It is also fail-fast. But it does not have any remove method.


 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abhineet Kapil wrote:Iterator : It is fail-fast. That is it fails immediately if an attempt is made to modify a collection while iterating over it.
Although modification (remove elements) can be made in the collection through Iterators remove method only.
Enumerator : It is also fail-fast. But it does not have any remove method.

Mmmm, there are several problems here. Aside from the fact that this thread is over a year old, and already answered pretty well. But as long as we're bringing it back for some reason:

Both Iterator and Enumeration can be fail-fast. But they don't have to be. And whether they are or not depends on the specific class implementation you're using. Also maybe on the JDK version in some cases. For example an Enumeration from a Vector does not throw any ConcurrentModificationException. Nor does an Iterator from a List returned by Arrays.asList().

Also, there can be ways to modify a fail-fast collection without using remove() and without any ConcurrentModificationException. For example, using the set() method in ArrayList or LinkedList.

Even when using a class and method that can throw ConcurrentModificationException, the documentation points out that it is in general hard to guarantee that a ConcurrentModificationException will be thrown if inappropriate modification occurs. Especially if it happens from different threads. Weird things can happen when multiple threads are involved. In general, you can say that a ConcurrentModificationException will probably be thrown if illegal concurrent modification occurs, when using a fail-safe class and method. But we can't really say it will be thrown. And more generally, if you're not sure exactly which class and method implementation(s) you're talking about, then it's best to just say that a ConcurrentModificationException may be thrown. There are a lot of possible exceptions out there, and it's difficult to speak in absolutes.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic