Fail-fast
实际上,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深有体会
Fail-fast的更多相关文章
- Fail Fast and Fail Safe Iterators in Java
https://www.geeksforgeeks.org/fail-fast-fail-safe-iterators-java/ Fail Fast and Fail Safe Iterators ...
- fail fast和fail safe策略
优先考虑出现异常的场景,当程序出现异常的时候,直接抛出异常,随后程序终止 import java.util.ArrayList; import java.util.Collections; impor ...
- 【问题】Could not locate PropertySource and the fail fast property is set, failing
这是我遇到的问题 Could not locate PropertySource and the fail fast property is set, failing springcloud的其他服务 ...
- 快速失败(fail—fast)和 安全失败(fail—safe)
快速失败(fail-fast) 在用迭代器遍历一个集合对象时,如果遍历过程中对集合对象的结构进行了修改(增加.删除),则会抛出Concurrent Modification Exception. 原理 ...
- Java集合框架中的快速失败(fail—fast)机制
fail-fast机制,即快速失败机制,是java集合框架中的一种错误检测机制.多线程下用迭代器遍历一个集合对象时,如果遍历过程中对集合对象的内容进行了修改(增加.删除),则会抛出Concurre ...
- Storm介绍(二)
作者:Jack47 转载请保留作者和原文出处 欢迎关注我的微信公众账号程序员杰克,两边的文章会同步,也可以添加我的RSS订阅源. 本文是Storm系列之一,主要介绍Storm的架构设计,推荐读者在阅读 ...
- DotNet 资源大全中文版(Awesome最新版)
Awesome系列的.Net资源整理.awesome-dotnet是由quozd发起和维护.内容包括:编译器.压缩.应用框架.应用模板.加密.数据库.反编译.IDE.日志.风格指南等. 算法与数据结构 ...
- Android 5.X新特性之为RecyclerView添加下拉刷新和上拉加载及SwipeRefreshLayout实现原理
RecyclerView已经写过两篇文章了,分别是Android 5.X新特性之RecyclerView基本解析及无限复用 和 Android 5.X新特性之为RecyclerView添加Header ...
- Favorites of top 10 rules for success
Dec. 31, 2015 Stayed up to last minute of 2015, 12:00am, watching a few of videos about top 10 rules ...
- C++ Tips and Tricks
整理了下在C++工程代码中遇到的技巧与建议. 0x00 巧用宏定义. 经常看见程序员用 enum 值,打印调试信息的时候又想打印数字对应的字符意思.见过有人写这样的代码 if(today == MON ...
随机推荐
- 搭建本地YUM仓库
YUM介绍 yum(yellow dog updater modified)为多个Linux发行版的软件包管理工具,Redhat RHEL CentOS Fedora YUM主要用于自动安装,升级rp ...
- 层叠机制和继承的概念以及CSS中选择器的优先级
层叠机制: 一个元素的某个特定的样式属性可能来自行间的style属性.内联样式表或者外部引入的样式表,以及浏览器自定义的样式,还有就是继承自父元素的样式,但是最终只会选择其中的某一个来表示,这个选择的 ...
- SpringBoot初探JSP页面可能遇到的坑
第一个坑就是依赖没有配了 网上很多依赖的配置代码 在pom.xml文件的dependencies添加以下依赖 <!-- servlet依赖 --> <dependency> & ...
- Ubuntu服务器登录与使用
1. 登录 从本地登录远程服务器 1.1 默认端口 # format: ssh user_name@ip_address cv@cv: ~$ ssh cv@192.168.1.1 1.2 登录到指定端 ...
- Java语法进阶10-泛型
泛型 泛型:参数化的类型,即把数据类型当做参数来传递 有的地方又称为泛化的类型,用一个单个大写字母,例如<T>来代表任意类型,这个T就是泛化的类型. 泛型的好处: (1)表示某个变量的类型 ...
- Flutter - You need to use a different version code for your APK or Android App Bundle because you already have one with version code 1.
前两天提交了一个版本Google Play,结果今天收到拒绝的邮件,说App内购有问题. 于是把设置里面的支付宝和微信打赏功能关闭,又打了一个aab. 然后上传到Google Play,结果提示 Yo ...
- 服务发现(consul)搭建
服务发现(consul)搭建 下载最新版 consul 本人使用的版本为1.5.1,操作系统:window server 2008 consul部署的时候分为客户端和服务端,本次操作服务器2台,客户端 ...
- Hadoop2.8分布式集群安装与测试
1.hadoop2.x 概述 个).每一个都有相同的职能.一个是active状态的,一个是standby状态的.当集群运行时,只有active状态的NameNode是正常工作的,standby状态的N ...
- Nezuko: 1 Vulnhub Walkthrough
下载地址: https://www.vulnhub.com/entry/nezuko-1,352/ 虚拟机启动,设置IP地址DHCP获取 主机发现扫描: 主机层面扫描: ╰─ nmap -p1-655 ...
- centos7安装python3.7.4
yum install gcc make zlib zlib-devel openssl openssl-devel libffi-devel bzip2-devel ncurses-devel g ...