This lesson explains why ConcurrentModificationExceptions occurs and how it can be avoided.

If you are interviewing, consider buying our number#1 course for Java Multithreading Interviews.

Single Thread Environment

The name ConcurrentModificationException may sound related to concurrency, however, the exception can be thrown while a single thread operates on a map. In fact, ConcurrentModificationException isn’t even part of the java.util.concurrent package. The exception occurs when a map is modified at the same time (concurrently) any of its collection views (keys, values or entry pairs) is being traversed. The program below demonstrates the exception being thrown as the main thread traverses the map entries and also attempts to insert new entries.

Create a free account to view this lesson.

By signing up, you agree to Educative's Terms of Service and Privacy Policy