---
title: "什么是fail-fast"
author: "jaywee"
date: "2008-11-09T11:42:41Z"
tags: []
slug: "fail-fast"
draft: false
lastmod: "2008-11-09T11:45:43Z"
id: 264573
url: "https://jaywee.iteye.com/blog/264573"
---

if the list is structurally modified at any time after the iterator is created, in any way except through the Iterator's own `remove` or `add` methods, the iterator will throw a `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.

在迭代器创建之后，如果从结构上对列表进行修改，除非通过迭代器自身的 `remove` 或 `add` 方法，其他任何时间任何方式的修改，迭代器都将抛出 `ConcurrentModificationException` 。因此，面对并发的修改，迭代器很快就会完全失败，而不冒将来不确定的时间任意发生不确定行为的风险。

注意，迭代器的快速失败行为不能得到保证，一般来说，存在不同步的并发修改时，不可能作出任何硬性保证。快速失败迭代器尽最大努力抛出 `ConcurrentModificationException` 。因此，编写依赖于此异常的程序的方式是错误的，正确做法是：*迭代器的快速失败行为应该仅用于检测程序错误。*