Register now or log in to join your professional community.
In Concurrent Hash Map.
FailSafe Transactions - It makes a copy of the internal data structure (object array) and iterates over the copied data structure.Any structural modification done to the iterator affects the copied data structure therefore the original datastructure remains structurally unchanged .With this , no ConcurrentModificationException is thrown.
FailFast Transctions - When one or more thread is iterating over the collection, in between, one thread changes the structure of the collection (either adding the element to the collection or by deleting the element in the collection or by updating the value at particular position in the collection) is known as Concurrent Modification Fail fast Iterator.
This talks about Iterator behaviour while iterate through collection.
Fail fast Iterator
While iterating through the collection , instantly throws Concurrent Modification Exception if there is structural modification of the collection . Thus, in the case of concurrent modification, the iterator fails quickly and cleanly.
Fail-fast iterator can throw ConcurrentModificationException in two scenarios :
Single Threaded Environment
After the creation of the iterator , structure is modified at any time by any method other than iterator's own remove method.
Multiple Threaded Environment
If one thread is modifying the structure of the collection while other thread is iterating over it .
Fail Safe Iterator :
Fail Safe Iterator makes copy of the internal data structure (object array) and iterates over the copied data structure.Any structural modification done to the iterator affects the copied data structure. So , original data structure remains structurally unchanged .Hence , no ConcurrentModificationException throws by the fail safe iterator.
Two issues associated with Fail Safe Iterator are :
1. Overhead of maintaining the copied data structure
2. Fail safe iterator does not guarantee that the data being read is the data currently in the original data structure.
Fail-Fast
Iterator can access the internal data structure directly.The internal data structure should not be modified whilie Iterating overt the collections.To ensure iterator maintain "mod" flag.Iterator checks the "mod" flag whenever it gets the next value hasNext(). value of "mod" changes whenever there is an structure modification.
Fail-Safe
Fail-Safe makes a copy of internal data structure and it iterates over the copies data structure.So,there is no modification in internal data structure.