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 ...

"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:

"Most concurrent Collection implementations (including most Queues) also differ from the usual java.util conventions in that their Iterators and Spliterators provide weakly consistent rather than fast-fail traversal."

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.)

"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.

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.

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.

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

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 - 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:

"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."

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值