快速失败 (fail-fast) 和安全失败 (fail-safe) 的区别是什么?

描述

java.util包下面的所有集合类都是快速失败的,而Java.util.concurent包下面的集合类都是安全失败。
快速失败 迭代器会抛出ConcurentModificationException异常,而安全失败的迭代器永远 不会抛出这样的异常

快速失败(fail-fast)

public class FailFastDemo {
    public static void main(String[] args) {
        HashMap hashMap = new HashMap();
        hashMap.put("111", 1);
        hashMap.put("222", 2);
        hashMap.put("333", 3);

        Set set = hashMap.entrySet();
        Iterator iterator = set.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
            hashMap.put("下次循环会抛异常", 4);
            System.out.println("此时 hashMap 长度为" + hashMap.size());
        }

    }
}
111=1
此时 hashMap 长度为4
Exception in thread "main" java.util.ConcurrentModificationException
	at java.util.HashMap$HashIterator.nextNode(HashMap.java:1445)
	at java.util.HashMap$EntryIterator.next(HashMap.java:1479)
	at java.util.HashMap$EntryIterator.next(HashMap.java:1477)
	at com.zuojie.thread.demo4.FailFastDemo.main(FailFastDemo.java:17)
Disconnected from the target VM, address: '127.0.0.1:57770', transport: 'socket'

为什么在使用迭代器遍历集合时,修改集合会抛出异常?
原因是:迭代器在遍历集合时会直接访问集合中的内容,并且在遍历的过程中使用一个modCount的变量,集合在遍历期间如果发生了变化,就会改变modCount的值。
每当迭代器使用 hashNext()/next() 遍历下一个元素之前都会检测 modCount 变量是否为 expectedModCount 值,是的话就返回遍历;否则抛出异常,终止遍历。

安全失败(fail-safe)

采用安全失败机制的集合容器,在遍历时不是直接在集合内容上访问的,而是先复制原有的集合内容在拷贝的集合上进行遍历。
由于迭代时是对原集合的拷贝进行遍历,所以在遍历过程中对原集合所作的修改并不能被迭代器检测到,故不会抛 ConcurrentModificationException 异常

public class FailSafeDemo {
    public static void main(String[] args) {
        ConcurrentHashMap concurrentHashMap = new ConcurrentHashMap();
        concurrentHashMap.put("111", 1);
        concurrentHashMap.put("222", 2);
        concurrentHashMap.put("3333", 3);

        Set set = concurrentHashMap.entrySet();
        Iterator iterator = set.iterator();

        while (iterator.hasNext()) {
            System.out.println(iterator.next());
            concurrentHashMap.put("下次循环正常执行", 4);
        }
        System.out.println("程序结束");
    }
}
111=1
222=2
3333=3
程序结束

Process finished with exit code 0

最后说明一下,快速失败和安全失败是对迭代器而言的。并发环境下建议使用 java.util.concurrent 包下的容器类,除非没有修改操作。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值