fail fast和fail safe策略
优先考虑出现异常的场景,当程序出现异常的时候,直接抛出异常,随后程序终止
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List; class Main{
public static void main(String[] args) {
List<String> list = new ArrayList<>();
Collections.addAll(list,"A","B","C","D");
//modCount = 4,从0开始
Iterator<String> iterator = list.iterator();//取得集合迭代器(取得当前集合的副本),expectedModCount = 4
//当取得集合迭代器的时候(及调用list.iterator()),expectedModCount=modCount
while(iterator.hasNext()){
String str = iterator.next();//每次调用这句代码时候 会调用checkForComodification()
//以此来检查副本中的expectedModCount是否等于集合中的modCount,为了安全考虑(保证不会脏读)
if(str.equals("B")){
//list.remove("B"); //modCount +1=5 ConcurrentModificationException
iterator.remove();
continue;
}
}
}
}
fail-fast机制
ConcurrentModificationException,Collection集合使用迭代器遍历的时候,使用了集合类提供的修改集合内容方法报错
产生原因:
final void checkForComodification() {
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
}
collection集合中的modCount表示当前集合修改的次数(remove,add调用的时候,modCount++)
在并发使用的场景中如果发生值的修改保证用户独到的是当前集合的最新值,而不会发生脏读
当取得集合迭代器的时候(及调用list.iterator()),expectedModCount=modCount
换言之,迭代器就是当前集合的一个副本
而如果是有了Iterator迭代器提供的remove()不会出现此错误,本质上在remove时候将modCount重新赋值给expectedModCount
fail safe集合
不抛出此异常的集合
fail-safe:不产生ConcurrentModificationException异常
juc包下线程安全的集合类(CopyOnWriteArrayList、ConcurrentHashMap)
总结:以后在迭代器遍历的时候,不要修改集合的内容
1.为何产生fail fast?
ModCount expectedModCount存在于内部迭代器实现,存储当前集合修改次数
modCount存在于AbstractList记录List集合被修改(add remove)的次数
2.fail fast的意义:保证多线程场景下读取数据不会发生脏读,当有一个线程修改集合的时候,告诉客户端拿到的数据并非最新的
当执行list.remove()的时候,判断元素的下标,使用fastRemove,此时modCount++,不再等于expectedModCount
fail fast和fail safe策略的更多相关文章
- 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)
快速失败(fail-fast) 在用迭代器遍历一个集合对象时,如果遍历过程中对集合对象的结构进行了修改(增加.删除),则会抛出Concurrent Modification Exception. 原理 ...
- 【问题】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的其他服务 ...
- Java集合框架中的快速失败(fail—fast)机制
fail-fast机制,即快速失败机制,是java集合框架中的一种错误检测机制.多线程下用迭代器遍历一个集合对象时,如果遍历过程中对集合对象的内容进行了修改(增加.删除),则会抛出Concurre ...
- java中fail-fast 和 fail-safe的区别
java中fail-fast 和 fail-safe的区别 原文地址:http://javahungry.blogspot.com/2014/04/fail-fast-iterator-vs-fa ...
- java fail-fast和fail-safe
快速失败(fail—fast) 在用迭代器遍历一个集合对象时,如果遍历过程中对集合对象的内容进行了修改(如增加.删除等),则会抛出Concurrent Modification Exception. ...
- Netty学习路线总结
序 之前开过品味性能系列.Mysql学习系列,颇为曲高和寡.都是讲理论,很少有手把手深入浅出的文章.不过确实我就这脾气,文雅点的说法叫做"伪雅",下里巴人叫做"装逼&qu ...
- 面试题思考:java中快速失败(fail-fast)和安全失败(fail-safe)的区别是什么?
一:快速失败(fail—fast) 在用迭代器遍历一个集合对象时,如果遍历过程中对集合对象的内容进行了修改(增加.删除.修改),则会抛出Concurrent Modification Exceptio ...
- java中快速失败(fail-fast)和安全失败(fail-safe)的区别是什么?
一:快速失败(fail—fast) 在用迭代器遍历一个集合对象时,如果遍历过程中对集合对象的内容进行了修改(增加.删除.修改),则会抛出Concurrent Modification Exceptio ...
随机推荐
- mysql系列2 权限相关
mysql授权认证 请注意(大坑):mysql8.0以前的版本可以使用grant在授权的时候隐式的创建用户,8.0以后已经不支持,所以必须先创建用户,然后再授权!! 例子: 在170mysql主机上授 ...
- xadmin引入django-rest-framework
一.安装: pip install djangorestframework 安装djangorestframework库 https://github.com/encode/django-rest-f ...
- xadmin引入django-stdimage在列表页预览图片
一.安装 pip install django-stdimage 安装django-stdimage库 https://github.com/codingjoe/django-stdimage Git ...
- 可变lambda, lambda使用mutable关键字
关于lambda的捕获和调用 C++ primer上对可变lambda举的例子如下: size_t v1=42; auto f=[v1] () mutable{return ++v1; }; v1=0 ...
- HDU3507:Print Article(斜率优化dp)
传送门 题意: 现有\(n\)个数,每个数的值为\(a_i\),现在可以把数划分为多段,每一段的代价为\((\sum_{k=i}^{j}c_i)^2+M\). 问怎么划分,代价最小. 思路: 考虑dp ...
- chromedriver对应chrome版本
chromedriver版本 支持的Chrome版本 v2.41 v67-69 v2.40 v66-68 v2.39 v66-68 v2.38 v65-67 v2.37 v64-66 v2.36 v6 ...
- python gevent协程
安装 pip install gevent import gevent from gevent import monkey monkey.patch_all()#捕捉所有阻塞,不止接收gevent.s ...
- SpreadJS 生成报表
空了再写个完整的demo吧 //报表控件 输入参数待定 function SpreadObj(response) { var spread = null; //数据列表 var dataArray = ...
- python3 ini文件读写
import configparser config = configparser.ConfigParser() file = 'config.ini' config.read(file) confi ...
- display:none和visibility:hidden
w3school学习网:https://www.w3school.com.cn/tiy/t.asp?f=hdom_style_display_none display: none----将元素的显示设 ...