实际上,java.util.Iterator 的大多数实现都提供了故障快速修复(Fail-fast)的机制 ⎯⎯在利用迭代器遍历某一容器的过程中,一旦发现该容器的内容有所改变,迭代器就会抛出 ConcurrentModificationException 意外错并立刻退出。

public class FailFast {
public static void main(String[] args) {
List<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);
list.add(3);
Iterator iterator = list.iterator();
while (iterator.hasNext()) {
iterator.next();
list.add(4);
}
}
}

输出

Exception in thread "main" java.util.ConcurrentModificationException
at java.base/java.util.ArrayList$Itr.checkForComodification(ArrayList.java:1009)
at java.base/java.util.ArrayList$Itr.next(ArrayList.java:963)
at sort.FailFast.main(FailFast.java:16)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:567)
at com.intellij.rt.execution.application.AppMainV2.main(AppMainV2.java:131)

Wiki

Fail-fast systems or modules are desirable in several circumstances:

  • When building a fault-tolerant system by means of redundant components, the individual components should be fail-fast to give the system enough information to successfully tolerate a failure.
  • Fail-fast components are often used in situations where failure in one component might not be visible until it leads to failure in another component.
  • Finding the cause of a failure is easier in a fail-fast system, because the system reports the failure with as much information as possible as close to the time of failure as possible. In a fault-tolerant system, the failure might go undetected, whereas in a system that is neither fault-tolerant nor fail-fast the failure might be temporarily hidden until it causes some seemingly unrelated problem later.
  • A fail-fast system that is designed to halt as well as report the error on failure is less likely to erroneously perform an irreversible or costly operation.

Developers also refer to fail-fast code to a code that tries to fail as soon as possible at variable or object initialization. In OOP, a fail-fast designed object initializes the internal state of the object in the constructor, launching an exception if something is wrong (vs allowing non-initialized or partially initialized objects that will fail later due to a wrong "setter"). The object can then be made immutable if no more changes to the internal state are expected. In functions, fail-fast code will check input parameters in the precondition. In client-server architectures, fail-fast will check the client request just upon arrival, before processing or redirecting it to other internal components, returning an error if the request fails (incorrect parameters, ...). Fail-fast designed code decreases the internal software entropy, and reduces debugging effort.

实际上immutable和variable不是一个意思,但是中文翻译总是不可变。可见,要学英语放置被误导。这点在我学java的函数式编程时的copy by value深有体会

最新文章

  1. .NET平台下IIS7.5+无后缀名伪静态实现办法
  2. 丰富Easyui 的插件 - lookup
  3. angular ui-router 缓存问题
  4. CSS background 属性
  5. MySQL优化经验和方法汇总
  6. iOS 证书调试的理解(Personal)
  7. 使用ASP.NET操作IIS7中使用应用程序
  8. AS3.0声明静态属性和静态方法
  9. 新闻源图片放到js里
  10. JSON &amp; XML 简析
  11. [wiki] Unix like
  12. 阶段02JavaWeb基础day04mysql
  13. DICOM医学图像处理:WEB PACS初谈四,PHP DICOM Class
  14. How to return plain text from AWS Lambda &amp; API Gateway
  15. C语言__LINE__实现原理
  16. iOS 适配/ autoLayout基本知识
  17. 剑指Offer - 九度1371 - 最小的K个数
  18. linux 时间与本地时间不对应解决办法
  19. 5.7 并行复制配置 基于GTID 搭建中从 基于GTID的备份与恢复,同步中断处理
  20. 给ACM newer的编程技巧

热门文章

  1. numpy的基本API(三)——索引
  2. bash单引号嵌套
  3. springboot+k8s+抛弃springcloud.eureka
  4. 如何在JS代码中消除for循环
  5. 【HttpClient】使用学习
  6. 70道阿里百度高频Java面试题(框架+JVM+多线程+算法+数据库)
  7. nginx配置隐藏index.php
  8. 在MySQL中group by 是什么意思
  9. 2016/10/21 java中的参数传方式
  10. 安装完PyCharm,启动时弹出Failed to load JVM DLLbinserverjvm