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.
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.)
“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.)
相关文章推荐
- js 中json字符串转化json对象
- jquery的事件绑定
- html可拖拽元素
- angularjs事件指令
- 【剑指offer】题目20 顺时针打印矩阵
- FastJson的使用
- 原生js 异步请求,responseXML解析
- 移动设备、手机浏览器Javascript滑动事件代码
- JSP内置对象与servlet对应关系
- 不要使用SBJSON(json-framework)
- jQuery.extend 函数详解
- HTML5 五大特性
- ubuntu nodejs + express4.x 安装 测试
- google protocol buffer的原理和使用(四)
- js 网页各种定位
- Highcharts + jQuery + Servlet 实现从后台获取JSON实时刷新图表
- 基于jquery鼠标点击图片翻开切换效果
- jquery的$(#id).each()和jquery.each()的用法区别
- google protocol buffer的原理和使用(三)
- JavaScript数据类型转换