Java / Collections
Difference between fail-fast and fail-safe Iterators.
Fail-fast Iterators throws ConcurrentModificationException when one thread is iterating over the collection object and other thread structurally modify the Collection either by adding, removing or modifying objects on underlying collection. They are called fail-fast because it immediately throws Exception when they encounter modification. On the other hand, fail-safe Iterators works on a copy of the collection instead of the original collection.
| Fail Fast Iterator. | Fail Safe Iterator. |
| Fail Fast iterator throws ConcurrentModificationException. | Fail Safe doesn't throw. |
| Doesn't clone; works directly on the collection object. | Creates a copy of collection by cloning. |
| Doesn't cause memory overhead. | causes memory overhead. |
| Examples: HashMap, Vector, ArrayList and HashSet. | Examples: CopyOnWriteArrayList, ConcurrentHashMap, and ConcurrentLinkedQueue. |
More Related questions...