什么是故障安全& Java中的失败快速迭代器 [英] What are fail-safe & fail-fast Iterators in Java

查看:196
本文介绍了什么是故障安全& Java中的失败快速迭代器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java中有两种类型的迭代器:故障安全和快速失败。

There are two types of iterators in Java: fail-safe and fail-fast.

这是什么意思,它们之间有什么区别?

What does this mean, and is the difference between them?

推荐答案


它们之间的区别是什么......

What is the difference between them ...

失败安全是指:它不会失败。严格地说,Java中的没有这样的东西作为故障安全迭代器。正确的术语是弱一致。 javadoc说:

"Fail safe" means: it won't fail. Strictly speaking, there is no such thing in Java as a fail-safe iterator. The correct term is "weakly consistent". The javadoc says:


大多数并发Collection实现(包括大多数队列)也与通常的java.util约定不同他们的迭代器和Spliterators提供弱一致而不是快速失败的遍历。

通常,弱一致意味着如果a集合与迭代同时修改,迭代看到的保证较弱。 (详细信息将在每个并发集合类javadocs中指定。)

Typically, weak consistency means that if a collection is modified concurrently with an iteration, the guarantees of what the iteration sees are weaker. (The details will be specified in each conncurrent collection classes javadocs.)

快速失败意味着:可能失败......并且严重检查故障条件,以便在损坏完成之前检测到的故障情况(如果可能 1 )。在Java中,失败快速迭代器失败,抛出 ConcurrentModificationException

"Fail fast" means: it may fail ... and the failure condition is checked aggressively so that the failure condition is (where possible1) detected before damage can be done. In Java, a fail-fast iterator fails by throwing a ConcurrentModificationException.

替代fail-fast并且弱一致是迭代失败不可预测的语义;例如有时给出错误的答案或抛出一个完全出乎意料的例外。 (这是早期Java版本中 Enumeration API的一些标准实现的行为。)

The alternative to "fail-fast" and "weakly consistent" is a semantic where the iteration fails unpredictably; e.g. to sometimes give the wrong answer or throw a totally unexpected exception. (This was the behavior of some standard implementations of the Enumeration API in early versions of Java.)


...它们与我们用于收集的迭代器不同。

... and are they different from the iterator we use for collection.

不。这些是标准Collection类型实现的迭代器的属性;即,它们要么快速失败,要么弱一致......正确使用同步和Java内存模型 1

No. These are properties of the iterators implemented by standard Collection types; i.e. they are either "fail fast" or "weakly consistent" ... when used correctly with respect to synchronization and the Java memory model1.

故障快速迭代器通常使用集合对象上的 volatile 计数器实现。

The fail-fast iterators are typically implemented using a volatile counter on the collection object.


  • 更新集合时,计数器会递增。

  • 创建 Iterator 时,计数器的当前值嵌入在 Iterator object。

  • 当执行 Iterator 操作时,该方法会比较两个计数器值并抛出CME是不同的。

  • When the collection is updated, the counter is incremented.
  • When an Iterator is created, the current value of the counter is embedded in the Iterator object.
  • When an Iterator operation is performed, the method compares the two counter values and throws a CME if they are different.

故障安全迭代器的实现通常很轻。它们通常依赖于特定列表实现的数据结构的属性。没有一般模式。 (阅读您感兴趣的特定集合类的源代码。)

The implementation of fail-safe iterators is typically light-weight. They typically rely on properties of the specific list implementation's data structures. There is no general pattern. (Read the source code for the specific collection classes you are interested in.)

1 - 骑手是快速失败行为假定应用程序在同步和内存模型方面正确。这意味着(例如)如果在没有正确同步的情况下迭代 ArrayList ,最终结果可能是损坏的列表结果。 快速失败机制可能会检测到并发修改(尽管不能保证),但它不会检测到潜在的损坏。例如, javadoc 对于 Vector.iterator()这样说:

1 - The rider is that fail-fast behavior assumes that the application id correctly with respect to synchronization and the memory model. That means that (for example) if you iterate an ArrayList without proper synchronization, the end result could be a corrupted list result. The "fast fail" mechanism will probably detect the concurrent modification (though that isn't guaranteed), but it won't detect the underlying corruption. As an example, javadoc for Vector.iterator() says this:


迭代器的快速失败行为无法得到保证,因为一般来说,在存在非同步并发修改的情况下,不可能做出任何硬性保证。失败快速迭代器抛出 ConcurrentModificationException 在尽力而为的基础上。因此,编写一个依赖于此异常的程序以确保其正确性是错误的:迭代器的故障快速行为应该仅用于检测错误。

"The fail-fast behavior of an iterator cannot be guaranteed as it is, generally speaking, impossible to make any hard guarantees in the presence of unsynchronized concurrent modification. Fail-fast iterators throw ConcurrentModificationException on a best-effort basis. Therefore, it would be wrong to write a program that depended on this exception for its correctness: the fail-fast behavior of iterators should be used only to detect bugs."

这篇关于什么是故障安全& Java中的失败快速迭代器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆