什么是故障安全&Java中的快速失败迭代器

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

问题描述

Java 中有两种类型的迭代器:fail-safe 和 fail-fast.

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 中不存在作为故障安全迭代器的东西.如果迭代器失败(通常意义上的失败"),您可以预期会发生损坏.

"Fail-safe" (in engineering) means that something fails in a way that causes no or minimal damage. Strictly speaking, there is no such thing in Java as a fail-safe iterator. If an iterator fails (in the normal sense of "fail"), you can expect damage to occur.

我怀疑你的意思实际上是弱一致".迭代器.javadoc 说:

I suspect that you actually mean "weakly consistent" iterators. The javadoc says:

大多数并发 Collection 实现(包括大多数队列)也不同于通常的 java.util 约定,因为它们的 Iterators 和 Spliterators 提供弱一致而不是快速失败遍历."p>

通常,弱一致性意味着如果集合与迭代同时被修改,则迭代所看到的内容的保证会更弱.(详细信息将在每个并发集合类 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 concurrent collection classes javadocs.)

快速失败"(在系统设计中)表示积极检查故障条件,以便故障条件(在可能的情况下1)在造成过多损坏之前检测到.在 Java 中,快速失败的迭代器会因抛出 ConcurrentModificationException 而失败.

"Fail-fast" (in systems design) means that the failure condition is checked aggressively so that the failure condition is (where possible1) detected before too much damage can be done. In Java, a fail-fast iterator fails by throwing a ConcurrentModificationException.

快速失败"的替代方案;和弱一致"是迭代失败不可预测的语义;例如有时给出错误的答案或抛出意外的异常.(这是早期 Java 版本中 Enumeration API 的一些标准实现的行为.)

The alternative to "fail-fast" and "weakly consistent" is semantic where the iteration fails unpredictably; e.g. to sometimes give the wrong answer or throw an 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.

没有.这些是标准集合类型实现的迭代器的属性;即它们要么快速失败",要么快速失败".或弱一致"... 在同步和 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 计数器实现.

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

  • 当集合更新时,计数器会增加.
  • 创建Iterator时,计数器的当前值嵌入到Iterator对象中.
  • 当执行 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.

相比之下,弱一致性迭代器通常是轻量级的,并利用每个并发集合的内部数据结构的属性.没有通用模式.如果您有兴趣,请阅读不同集合类的源代码.

By contrast, weakly consistent iterators are typically light-weight and leverage properties of each concurrent collection's internal data structures. There is no general pattern. If you are interested, read the source code for different collection classes.

1 - 快速失败迭代器行为假设应用程序在同步和内存模型方面已正确实现.(换句话说,应用程序 是线程安全的.)例如,如果您在没有正确同步的情况下迭代 ArrayList,快速失败"会导致机制应该检测并发修改(尽管不能保证),但可能无法防止由于应用程序的不安全行为而损坏列表.为了说明,javadoc for Vector.iterator() 是这样说的:

1 - The rider is that fail-fast iterator behavior assumes that the application is correctly implemented with respect to synchronization and the memory model. (In other words, the application is thread-safe.) For example, if you iterated an ArrayList without proper synchronization, the "fast fail" mechanism should detect the concurrent modification (though that isn't guaranteed), but may not prevent the list from being corrupted due to the application's unsafe behavior. To illustrate, the 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中的快速失败迭代器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:迭代器和Listiterator的区别? 下一篇:Iterator 的 remove 方法实际上是如何删除一个对象的

相关文章

最新文章