Hello everyone, we are back again with something cool.
Now the moment you read iterators, you say what is so different about that? We know most of you have read about iterators and what they do, how they work and other information
But wait! there's a catch!
As the title itself says 'Classified on basis of Concurrent Modification'. So wasting no more time lets look into iterators on a very different note.
Starting with the very first question "What is Concurrent Modification ?"
1. Concurrent Modification :
As the word describes itself, concurrent modification means performing two different operations on a structure at the same time. Say you have a collection that you want to iterate on but you perform some other operation on the collection while iterating which changed the structure of the collection then we call it concurrent modification. For example while iterating on a array list you add a value (or it can be a remove operation or a update operation) than this would refer to as concurrent modification of the collection.
In a more formal way, If one or more threads are performing operations on a collection such that the structure of the collection is changed by the other thread ( or the same thread) results in concurrent modification.
So we have discussed about concurrent modification and what it means.
Now this brings to our next question on what is its importance in java ?
2. Effect of Concurrent Modification on Iterators :
This is the most important part of the entire discussion. As we know, we have iterators which are used to iterate over a collection of elements. One specific thing about iterators is that they have two special types ( or properties to be specific). Iterators in java can follow two different patters.
1. Fail Fast
2. Fail Safe (Weekly Consistent)
1. Fail Fast :
By fail fast we mean that the iterator may fail while iterating through the elements.Lets take and example: We are iterating over an arraylist of integer values.Now if we try to modify the contents of the array list using collection methods (say by ArrayList.remove()) then the iterator throws ConcurrentModificationException. Thus as a result of concurrent modification, the iterator fails quickly instead of risking the changes in the structure of the collection which would result in undetermined changes in the future, the iterator throws an exception instantly.
To be very specific about what we are dealing with, here is what Oracle docs say :
"The fail-fast behavior of an iterator cannot be guaranteed as it is, generally speaking, impossible to make any hard guarantees in the presence of unsynchronized concurrent modification. Fail-fast iterators throw ConcurrentModificationException on a best-effort basis. Therefore, it would be wrong to write a program that depended on this exception for its correctness: the fail-fast behavior of iterators should be used only to detect bugs."
Thus the fail fast behavior cannot be relied on completely as for example if we are iterating on an array list with improper synchronization methods, we could end up with an corrupted list. The fail fast mechanism will detect the concurrent modification (though it is not guaranteed!) but wont detect the underlying corruption.
2. Fail Safe (Weekly Consistent) :
Weekly consistent simply means that the iterator wont fail while iterating. Strictly speaking the fail safe behavior is exhibited by concurrent Collections implementations i.e. Collections from the java.util.concurrent package implement weekly consistent iterators. Now how do these work ?
Basically a fail safe iterator works by making an copy of the internal data structure (for example the array of the ArrayList) and operates over the copied data structure.Hence the original data structure remains consistent without any changes and this explains why the fail safe iterator doesn't throw a ConcurrentModificationException. Any changes made during the iteration are performed on the copied data structure and hence the original structure remains unaltered.
Though the fail safe iterator does not guarantee that the data being read is the data that is currently present in the original collection.
Oracle docs :
"The "snapshot" style iterator method uses a reference to the state of the array at the point that the iterator was created. This array never changes during the lifetime of the iterator, so interference is impossible and the iterator is guaranteed not to throw ConcurrentModificationException. The iterator will not reflect additions, removals, or changes to the list since the iterator was created. Element-changing operations on iterators themselves (remove(), set(), and add()) are not supported. These methods throw UnsupportedOperationException."
That makes it clear that any changes wont be reflected because the iterator doesn't support any element changing operations at the first place.
Fail safe iterators examples are : Implementation in ConcurrentHashMap, CopyOnWriteArrayList etc.
You can read about iterators on the official oracle javadoc.
3. Conclusion:
So finally we can conclude that iterators in java can have two properties. They can fail fast or they can be fail safe.The implementation of the two differ based on weather they allow concurrent modification or not. One can easily test these concepts with simple java programs using a ArrayList for fail fast iterator and ConcurrentHashMap for fail safe iterator.We will provide the code in next revision.
So friends that was our discussion on the types of iterators based on the concurrent modification. Hope you enjoyed reading the post. And yes we will be back again with some really cool stuff.
For any queries please leave a comment below.
Stay Tuned and Keep Learning :)