java中的快速失败(fail-fast)与安全失败(fail-safe)
来源:互联网 发布:sublimetext3 mac 编辑:程序博客网 时间:2024/05/17 08:48
在我们详细讨论这两种机制的区别之前,首先得先了解并发修改。
1.什么是同步修改?
当一个或多个线程正在遍历一个集合Collection,此时另一个线程修改了这个集合的内容(添加,删除或者修改)。这就是并发修改
2.什么是 fail-fast 机制?
fail-fast机制在遍历一个集合时,当集合结构被修改,会抛出Concurrent Modification Exception。
fail-fast会在以下两种情况下抛出ConcurrentModificationException
(1)单线程环境
集合被创建后,在遍历它的过程中修改了结构。
注意 remove()方法会让expectModcount和modcount 相等,所以是不会抛出这个异常。
(2)多线程环境
当一个线程在遍历这个集合,而另一个线程对这个集合的结构进行了修改。
注意,迭代器的快速失败行为无法得到保证,因为一般来说,不可能对是否出现不同步并发修改做出任何硬性保证。快速失败迭代器会尽最大努力抛出 ConcurrentModificationException。因此,为提高这类迭代器的正确性而编写一个依赖于此异常的程序是错误的做法:迭代器的快速失败行为应该仅用于检测 bug。
3. fail-fast机制是如何检测的?
迭代器在遍历过程中是直接访问内部数据的,因此内部的数据在遍历的过程中无法被修改。为了保证不被修改,迭代器内部维护了一个标记 “mode” ,当集合结构改变(添加删除或者修改),标记"mode"会被修改,而迭代器每次的hasNext()和next()方法都会检查该"mode"是否被改变,当检测到被修改时,抛出Concurrent Modification Exception。下面看看ArrayList迭代器部分的源码
private class Itr implements Iterator<E> { int cursor; int lastRet = -1; int expectedModCount = ArrayList.this.modCount; public boolean hasNext() { return (this.cursor != ArrayList.this.size); } public E next() { checkForComodification(); /** 省略此处代码 */ } public void remove() { if (this.lastRet < 0) throw new IllegalStateException(); checkForComodification(); /** 省略此处代码 */ } final void checkForComodification() { if (ArrayList.this.modCount == this.expectedModCount) return; throw new ConcurrentModificationException(); } }可以看到它的标记“mode”为 expectedModeCount
4. fail-safe机制
fail-safe任何对集合结构的修改都会在一个复制的集合上进行修改,因此不会抛出ConcurrentModificationException
fail-safe机制有两个问题
(1)需要复制集合,产生大量的无效对象,开销大
(2)无法保证读取的数据是目前原始数据结构中的数据。
5. fail-fast 和 fail-safe的例子
import java.util.HashMap; import java.util.Iterator; import java.util.Map; public class FailFastExample { public static void main(String[] args) { Map<String,String> premiumPhone = new HashMap<String,String>(); premiumPhone.put("Apple", "iPhone"); premiumPhone.put("HTC", "HTC one"); premiumPhone.put("Samsung","S5"); Iterator iterator = premiumPhone.keySet().iterator(); while (iterator.hasNext()) { System.out.println(premiumPhone.get(iterator.next())); premiumPhone.put("Sony", "Xperia Z"); } } }输出:
iPhone Exception in thread "main" java.util.ConcurrentModificationException at java.util.HashMap$HashIterator.nextEntry(Unknown Source) at java.util.HashMap$KeyIterator.next(Unknown Source) at FailFastExample.main(FailFastExample.java:20)
import java.util.concurrent.ConcurrentHashMap; import java.util.Iterator; public class FailSafeExample { public static void main(String[] args) { ConcurrentHashMap<String,String> premiumPhone = new ConcurrentHashMap<String,String>(); premiumPhone.put("Apple", "iPhone"); premiumPhone.put("HTC", "HTC one"); premiumPhone.put("Samsung","S5"); Iterator iterator = premiumPhone.keySet().iterator(); while (iterator.hasNext()) { System.out.println(premiumPhone.get(iterator.next())); premiumPhone.put("Sony", "Xperia Z"); } } }输出:
S5HTC oneiPhone
6. fail-fast和 fail-safe 的区别
CopyOnWriteArrayList,
ConcurrentHashMap
7.总结,产生fail-fast的是在java.util包中的collection实现类;产生fail-safe的是在java.util.concurrent包中的collection实现类。
- java中的快速失败(fail-fast)与安全失败(fail-safe)
- java中的快速失败(fail-fast)与安全失败(fail-safe)
- 快速失败(fail-fast)和安全失败(fail-safe)
- java中的fail-fast(快速失败)机制
- Java学习笔记 14 快速失败fail-fast机制
- fail-fast快速失败机制分析
- Java Collection的Fail fast与Fail safe迭代器
- Fail Fast与Fail Safe的区别
- java Collection中fail-fast 、fail-safe
- java fail-fast 和 fail-safe
- Fail safe vs Fail fast
- Fail Fast Vs Fail Safe
- fail-fast(快速失败/报错机制)-ConcurrentModificationException
- “快速失败”也就是fail-fast,Java集合的一种错误检测机制。
- fail-fast 与 fail-safe 机制有什么区别
- java中fail-fast 和 fail-safe的区别
- java中fail-fast 和 fail-safe的区别
- Fail Fast And Fail Safe Iterators In Java
- Android碎片实例化方法:instantiate的使用及其源码解析
- SpringBoot的入门搭建(三)
- java的初识及入门
- Transition Framework
- java语言的概述
- java中的快速失败(fail-fast)与安全失败(fail-safe)
- 基于MATLAB的PCA人脸识别实现
- tomcat目录结构及配置文件说明
- R Notes
- 快速排序算法
- 前端基础练习题
- Django查询数据
- JVM
- Sum All Numbers in a Range