# Fail safe vs Fail fast

518人阅读 评论(0)

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

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：553112次
• 积分：10477
• 等级：
• 排名：第1656名
• 原创：380篇
• 转载：268篇
• 译文：1篇
• 评论：33条
文章分类
评论排行
最新评论