您的位置:首页 > Web前端

Fail safe vs Fail fast

2015-07-10 10:48 495 查看
“Fail safe” means: it won’t fail.

“Fail fast” means: it may fail(只是有可能,所以,有时也不会fail,例如你在使用iterator遍历到数组的最后一个元素时,数组被删除了一个元素,这时候是不会fail的) … and the failure condition is checked aggressively so that the failure condition is detectedbefore damage can be done.

The alternative to “fail safe” and “fail fast” is to fail unpredictably; e.g. to sometimes give the wrong answer or throw some unexpected exception. (This was the behaviour 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 “fail safe” … when used correctly with respect to synchronization and the Java memory model. (By contrast, if you use an non-concurrent Iterator or Collection without the correct external synchronization, all bets are off …)

Also what is the name of iterator we normally use to iterate.

Umm … the actual class name depends on how you got the Iterator, as do the Iterator’s properties; e.g. “fail fast” versus “fail safe”. For the standard classes and iterators obtained using the iterator() method, the properties are specified by the collection classes javadocs.

… how are they implemented.

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

* When the list 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 cases you are interested in.)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: