File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Iterator are fail safe while  Enumeration is not Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Iterator are fail safe while  Enumeration is not" Watch "Iterator are fail safe while  Enumeration is not" New topic
Author

Iterator are fail safe while Enumeration is not

Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

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 ??


Save India From Corruption - Anna Hazare.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38107
    
  22
I never knew iterators were thread safe; are they really? Please check your question.
Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

Sorry i mean to ask is Iterators are Fail safe .
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

Thanks for clarifying.
Abhineet Kapil
Ranch Hand

Joined: Feb 08, 2010
Posts: 52

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

Joined: Mar 05, 2008
Posts: 3003
    
    9
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Iterator are fail safe while Enumeration is not
 
Similar Threads
problems with hashset
Did the For loop lie
enumeratio or iterator
Help on applying Iterator
??? Difference Between Iterator & Enumeration ???