Quick revision
The short answer
ArrayList keeps a modCount field that every structural change, any change of size, increments. Its iterator copies that value when it is created and checks it in next() and remove(); a mismatch means the list was changed outside the iterator, and it throws ConcurrentModificationException. The check is best effort only: modCount is a plain field, so changes from other threads may not be visible and races can go undetected, which is why thread safety must come from concurrent collections or locking, never from this exception.
What to remember
In this answer, we saw that ArrayList counts structural changes in modCount, and its iterator compares that with expectedModCount in next() and remove(), throwing ConcurrentModificationException on a mismatch and resynchronising after its own remove(). The check is best effort: modCount is a plain field, so other threads' changes may be invisible, checks happen only at certain moments, and the list itself is not thread-safe. Thread safety comes from CopyOnWriteArrayList, synchronized wrappers with proper locking, concurrent queues, or not sharing mutable lists.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
ArrayList keeps a modCount field that every structural change, any change of size, increments. Its iterator copies that value when it is created and checks it in next() and remove(); a mismatch means the list was changed outside the iterator, and it throws ConcurrentModificationException. The check is best effort only: modCount is a plain field, so changes from other threads may not be visible and races can go undetected, which is why thread safety must come from concurrent collections or locking, never from this exception.
Why this question is asked
ConcurrentModificationException is one of the most misunderstood exceptions in Java. Its name suggests a thread-safety feature, and many developers treat its absence as proof that their code is safe. Interviewers ask about the mechanism to see whether we know what it really guarantees, which is very little. In this answer, we will look at the fields that make fail-fast work, follow an iterator through a check, see why it is reliable for single-threaded bugs but not for threads, and then look at the tools that actually provide thread safety.
1The Fields Behind Fail-Fast
Fail-fast means: detect a problem as early as possible and fail loudly, rather than continue with corrupted state. For collections, the problem is an iterator walking a list whose structure changed under it, which could make it skip elements, return one twice or read past the end.
ArrayList inherits a field called modCount from AbstractList. It is a plain int that counts structural modifications, changes that alter the size: add, remove, clear, addAll, removeIf and similar. A set(i, x) is not structural, because positions do not move, so it does not increase modCount. The iterator, an inner class of ArrayList, has three fields of its own: cursor, the index of the next element; lastRet, the index of the last element returned; and expectedModCount, a copy of modCount taken when the iterator was created.
| Field | Owner | Meaning |
|---|---|---|
modCount | ArrayList | count of structural changes so far |
expectedModCount | Iterator | modCount when the iterator was made |
| cursor | Iterator | index of the next element to return |
lastRet | Iterator | index last returned, -1 if none or removed |
2How the Check Works Step by Step
Each call to next() first runs a check: if modCount != expectedModCount, throw ConcurrentModificationException. Then it returns the element at cursor and advances. remove() on the iterator checks the same thing, removes the element at lastRet through the list, which increases modCount, and then sets expectedModCount = modCount, so the iterator stays in step with its own change. That is why it.remove() is safe while list.remove() inside the loop is not.
- modCount == expectedModCount?YesContinueNoThrow ConcurrentModificationException
- cursor >= size?YesThrow NoSuchElementExceptionNoRead elementData[cursor]
- cursor beyond array length?a sign of concurrent changeYesThrow ConcurrentModificationExceptionNoReturn the element
- Advance
lastRet= cursor, cursor++
4 steps in order. Play them, or step through them one at a time.
Notice two details. hasNext() does not check modCount on ArrayList, only cursor against size, which is why removing the second-last element in a for-each loop can end the loop silently instead of throwing. And the iterator also checks for an index beyond the array's length and throws ConcurrentModificationException in that case, a second safety net for inconsistent state.
3Why It Is Not a Reliable Concurrency Check
The documentation is explicit: fail-fast behaviour cannot be guaranteed and is done on a best-effort basis, so it should be used only to detect bugs, never relied on for correctness. With threads, there are three reasons.
First, visibility. modCount is a plain field, not volatile, and the iterator reads it without any synchronisation. Under the Java Memory Model, a write by one thread is only guaranteed to be visible to another after a happens-before relationship, such as a lock or a volatile write. Without one, the iterating thread may keep seeing an old modCount and never notice the change. Second, timing. Even if the value is visible, the check happens only at next() and remove(). Another thread can change the list just after a check, and the iterator reads elements during a resize or shift. Third, the list itself is not thread-safe. Two threads calling add() at the same time can both write to the same slot, losing one element, or one can see a size larger than the array and fail with ArrayIndexOutOfBoundsException. None of that is related to the iterator check.
4What to Use for Thread Safety
If a list is shared between threads, we choose a structure designed for it. CopyOnWriteArrayList suits read-mostly data: each change copies the array, and iterators work on a snapshot, so they never throw ConcurrentModificationException. Collections.synchronizedList wraps each method in a lock, but iteration is several calls, so the caller must hold the lock for the whole loop, as the documentation requires. For producer and consumer patterns, a queue from java.util.concurrent, such as ConcurrentLinkedQueue or a BlockingQueue, fits better than a list. And often the best design is to not share a mutable list at all: each thread builds its own, and we publish an immutable List.copyOf result.
So the exception is a useful alarm for single-threaded mistakes, such as removing inside a for-each loop. For threads, its absence proves nothing; correctness comes from the right collection or the right lock.
5Follow-up questions
Is set(i, x) during iteration safe?
In a single thread, yes. set is not a structural change, so modCount does not move and the iterator continues, returning the new value if it has not passed that index yet. Across threads, it is still a data race without synchronisation, so another thread may or may not see the new value.
Which collections do not fail fast?
The concurrent collections in java.util.concurrent have weakly consistent iterators: CopyOnWriteArrayList iterates a snapshot, and ConcurrentHashMap and ConcurrentLinkedQueue iterators never throw ConcurrentModificationException and may or may not reflect changes made during iteration. They trade the alarm for safe concurrent use.
Why is modCount not volatile, if that would make the check more reliable?
Because ArrayList is deliberately not thread-safe, and a volatile write on every add would slow down all single-threaded code for a check that still could not make the list safe. The check exists to catch programming errors cheaply, not to support concurrent use.
6Conclusion
In this answer, we saw that ArrayList counts structural changes in modCount, and its iterator compares that with expectedModCount in next() and remove(), throwing ConcurrentModificationException on a mismatch and resynchronising after its own remove(). The check is best effort: modCount is a plain field, so other threads' changes may be invisible, checks happen only at certain moments, and the list itself is not thread-safe. Thread safety comes from CopyOnWriteArrayList, synchronized wrappers with proper locking, concurrent queues, or not sharing mutable lists.
Open with
ArrayList inherits a field called modCount from AbstractList, and every structural change, meaning anything that changes the size like add, remove or clear, increments it. set does not, because positions stay the same.
Then, if they want more
When I create an iterator, it copies modCount into expectedModCount. Every next and every iterator remove first compares the two; if they differ, the list was changed by something other than this iterator, so its cursor might be wrong, and it throws ConcurrentModificationException.
The iterator's own remove removes through the list and then resets expectedModCount, which is why it.remove is safe and list.remove in a for-each loop is not. hasNext only compares the cursor with the size, which explains the case where removing the second-last element ends the loop silently.
The important point is that this is best effort, as the Javadoc says, and only meant to catch bugs. modCount is a plain int, not volatile, and read without synchronisation, so a change from another thread may never become visible to the iterating thread. And the check only runs at certain points, so a change can slip in between.
Besides, ArrayList itself is not thread-safe: concurrent adds can lose elements or throw ArrayIndexOutOfBounds. So for shared data I use the right tool: CopyOnWriteArrayList for read-mostly lists with snapshot iteration, a synchronizedList with the caller holding the lock while iterating, a concurrent queue, or immutable copies that are not shared mutably at all.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- The Collections Framework, overview, java.util, package documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "How does the fail-fast behaviour of ArrayList's iterator work, and why is...": https://scrivio-interview-library.netlify.app/java/lists/85188644db/