目录
FailFast机制的定义
Java中对非线程安全的容器的迭代器进行了限制。JDK中具体描述如下:
structurally modified at any time after the iterator is
* created, in any way except through the iterator's own
* {remove()} or
* {add(Object)} methods, the iterator will throw a
* {@link ConcurrentModificationException}. Thus, in the face of
* concurrent modification, the iterator fails quickly and cleanly, rather
* than risking arbitrary, non-deterministic behavior at an undetermined
* time in the future.
也就是说当在容器上创建了迭代器之后,如果调用了非迭代器提供的删除/添加等会修改容器内部数据结构的方法,譬如容器自身的添加/删除操作,那么迭代器在迭代过程中,检测到这个变化将抛出ConcurrentModificationException,导致迭代失败。