Skip to content
AITroveRead. Build. Understand.
Make this comfortable

Java LinkedList fail-fast iterators: a bug signal, not safety

Last updated: 1 Oct 20264 min read
tutorial
IntermediateBy AITrove Editorial

A LinkedList iterator may detect structural changes made outside that iterator and report ConcurrentModificationException.

The Java 8 program below compiles without external libraries; its output is checked against a real run.

The single-thread fixture exposes the mistake

The cursor reads one receipt, then the program appends another through the list. On this checked runtime the next cursor step throws. That is a diagnostic for a violated traversal contract. The original list still contains the new receipt.

A correct single-thread edit can use the iterator's own remove method, as in reverse traversal, or finish the traversal before mutating the list. Do not catch the exception and pretend the loop completed all items.

No concurrency guarantee follows

Oracle describes fail-fast behavior as best effort under unsynchronized concurrent change. It cannot be used to assert that all races are detected or that partial work rolled back. A shared queue needs a synchronization or ownership policy, not an exception handler.

Working program

Java
import java.util.Arrays;
import java.util.ConcurrentModificationException;
import java.util.Iterator;
import java.util.LinkedList;
public class ReceiptCursorInvalidation {
    public static void main(String[] args) {
        LinkedList<String> receipts = new LinkedList<>(Arrays.asList("R-31", "R-32"));
        Iterator<String> cursor = receipts.iterator();
        System.out.println("first=" + cursor.next());
        receipts.addLast("R-33");
        try { cursor.next(); }
        catch (ConcurrentModificationException invalidated) { System.out.println("cursor invalidated"); }
        System.out.println(receipts);
    }
}

Output

Output
first=R-31
cursor invalidated
[R-31, R-32, R-33]

Costs and boundaries

The example has no concurrent thread; it checks one deterministic misuse on the local runtime. Iterator traversal is O(n) for n elements. Exception detection is not an atomic snapshot or a synchronization mechanism.

Common Mistakes

  • Do not depend on fail-fast exceptions to prove absence of races.
  • Do not continue a partially processed batch as though traversal succeeded.
  • Do not make structural changes through the list while an iterator owns the edit path.

Read next

Java LinkedList: operations, internals and failure cases, Java LinkedList ListIterator: edit at a cursor without repeated searches, Java synchronizedList iteration: hold the wrapper lock.

Collection views and copy boundaries

Continue with Java 21 LinkedList.reversed: write-through order and endpoint edits, Java 21 access-order LinkedHashMap: reads change a reversed view.

List adapters and mutation boundaries

Continue with Java synchronized LinkedList iteration: hold the wrapper lock for the whole scan, Java LinkedList spliterator: traversal binds after creation.

java
linkedlist
linkedlist-failfast-boundary
Storage details