What is a fail-fast iterator?
Location: http://www.jguru.com/faq/view.jsp?EID=221988 Created: Oct 3, 2000 Modified: 2005-08-17 01:37:03.148 Author: Avi Kak (http://www.jguru.com/guru/viewbio.jsp?EID=26410) An iterator is considered fail-fast if it throws a ConcurrentModificationException under either of the following two conditions: • In multithreaded processing: if one thread is trying to modify a Collection while another thread is iterating over it. • In single-threaded or in multithreaded processing: if after the creation of the Iterator, the container is modified at any time by any method other than the Iterator’s own remove or add methods. Note in particular what is implied by the second condition: After we create a container’s iterator, during a loop that iterates over the container we must only use the remove (and when applicable add) methods defined for the iterator and that we must NOT use the same methods defined for the container itself. To illustrate this point, suppose we declare and initialize a List