Java.Collections.What is the difference between fail-fast and fail-safe?

Awesome question, Stanley — the difference between fail-fast and fail-safe is a classic concept in Java Collections, especially when you’re dealing with concurrent modifications.

Let’s make it super clear:


🧨 Fail-Fast

Fails immediately if the collection is structurally modified during iteration.

🔍 Characteristics:

  • Throws ConcurrentModificationException
  • Works on the original collection
  • Uses a modCount to track changes
  • Not thread-safe by default

🧪 Example:

List<String> list = new ArrayList<>();
list.add("Java");
list.add("Python");

for (String item : list) {
    list.remove(item); // ❌ throws ConcurrentModificationException
}

📌 Examples of fail-fast collections:

  • ArrayList
  • HashMap
  • HashSet
  • LinkedList

🛡️ Fail-Safe

Continues safely even if the collection is modified during iteration.

🔍 Characteristics:

  • No exceptions thrown
  • Iterates over a copy of the collection
  • Changes made during iteration don’t affect the iteration
  • Thread-safe in concurrent environments

🧪 Example using CopyOnWriteArrayList:

CopyOnWriteArrayList<String> list = new CopyOnWriteArrayList<>();
list.add("Java");
list.add("Python");

for (String item : list) {
    list.remove(item); // ✅ No exception thrown
}
System.out.println(list); // []

📌 Examples of fail-safe collections:

  • CopyOnWriteArrayList
  • ConcurrentHashMap
  • ConcurrentSkipListSet

🧠 Summary Table:

FeatureFail-FastFail-Safe
Reaction to modificationThrows exceptionNo exception, safe iteration
Iteration sourceOriginal collectionClone/copy of the collection
Thread-safe?NoYes (designed for concurrency)
Use caseSingle-threaded, performanceMulti-threaded, safety-first
Common inStandard collections (ArrayList)Concurrent collections (ConcurrentHashMap)

💬 Mnemonic:

🧨 Fail-Fast blows up fast.
🛡️ Fail-Safe plays it safe.


Want to explore the internal source code of a fail-safe vs fail-fast collection to see how it actually works under the hood? Or maybe try writing your own fail-fast iterator for learning?

This entry was posted in Без рубрики. Bookmark the permalink.