java的fail-fast 和 fail-safe机制

摘要:
这里的集合包括HashMap、Vector、ArrayList和HashSet。读取“main”java.util.CurrentModificationExceptionatjava.util.ArrayList$Itr.checkForCompodificationjava.util.ArryList$Itr.nextcom.raiseco.ems.pon.system.service.nemanager.impl.iscom6810.v4 _0.Test。分析主ArrayList$It的checkForModification断点:Iterator的remove方法不会引发ConcurrentModificationException。以下示例未引发异常。publicclassTest{publicstaticvoidmain{List<Integer>List=newArrayList<>();for{List.add;}Iterator<Integer˃it=List.Iterator();inttemp=0;而{if{temp++;//list.remove;it.remove();}else{temp++;System.out.println;}}}}}}将修改复制的集合,因此不会引发ConcurrentModificationException。不能保证读取的数据是当前原始数据结构中的数据。

  fail-fast机制是在遍历一个集合时,当集合结构被修改,可能会抛出ConcurrentModificationException。

  ArrayList源码中,Fail-fast iterators throw {@code ConcurrentModificationException} on a best-effort basis.

  快速失败迭代器会做出最大的努力来抛出ConcurrentModificationException。

  这里的集合包括:HashMap,Vector,ArrayList,HashSet。

  看下面的例子:

public class Test {
    public static void main(String[] args) {
        List<Integer> list = new ArrayList<>();
        for(int i = 0; i < 20; i++){
            list.add(i);
        }
        Iterator<Integer> it = list.iterator();
        int temp = 0;
        while(it.hasNext()){
            if(temp == 3){
                temp++;
                list.remove(3);
            }else{
                temp++;
                System.out.println(it.next());
            }
        }
    }
}

  这里的list.remove(3);会导致程序抛出ConcurrentModificationException。

Exception in thread "main" java.util.ConcurrentModificationException
    at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:901)
    at java.util.ArrayList$Itr.next(ArrayList.java:851)
    at com.raisecom.ems.pon.system.service.nemanager.impl.iscom6810.v4_0.Test.main(Test.java:22)

  ArrayList$Itr的checkForComodification打断点分析一下:

java的fail-fast 和 fail-safe机制第1张

   Iterator的remove方法不会抛出ConcurrentModificationException。 下面的例子不会抛出异常。

public class Test {
    public static void main(String[] args) {
        List<Integer> list = new ArrayList<>();
        for(int i = 0; i < 20; i++){
            list.add(i);
        }
        Iterator<Integer> it = list.iterator();
        int temp = 0;
        while(it.hasNext()){
            if(temp == 3){
                temp++;
                //list.remove(3);
                it.remove();
            }else{
                temp++;
                System.out.println(it.next());
            }
        }
    }
}

   fail-safe会在一个复制的集合上进行修改,因此不会抛出ConcurrentModificationException。无法保证读取的数据是目前原始数据结构中的数据。

   这里的集合包括:CopyOnWriteArrayList,ConcurrentHashMap,使用Collections.synchronizedList生成的集合。

public class Test2 {
    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");

        }
    }
}

  运行的结果是:

iPhone
HTC one
S5

免责声明:文章转载自《java的fail-fast 和 fail-safe机制》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇使用CSS画图之三角形(一)MAC 上找不到.bash_profile或者ect/profile该怎么办?下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Java精通并发-volatile与内存屏障的重要语义详细分析

在上一次https://www.cnblogs.com/webor2006/protected/p/12595201.html咱们已经对于volatile关键字的作用进行了一定的了解,这里回顾一下:   上一次对于第一条作用进行了详细的解读了,接下来则来解读一下剩下的两条:防止指令重排序、实现变量的可见性。而这俩其实都是通过一种手段来实现的:内存屏障(me...

网络技能大赛A卷测试

  这个测试对我来言有些难度,短时间内做不了太多。首先是思路的理清,登录后的界面有好几种,而且公文的状态也有好几种。理清思路就花了一些时间 然后大致的框架做了做,然后将用户的增删改查还有公文的增删改查写了写。登录界面也完成了,不过不同角色登陆后的界面还没来得及做。主要就是功能太多,运用不熟练 数据库      bean层的基本信息 package c...

Java如何正确的将数值转化为ArrayList?

Java中使用工具类Arrays.asList()看似可以把一个数组转为List,但实际使用时有两个坑:1、它是泛型方法,传入的参数必须是对象数组,当传入一个原生数据类型数组时,Arrays.asList() 的真正得到的参数就不是数组中的元素,而是数组对象本身。比如传入int[] intArray={1,2,3},它返回的list只有一个元素就是int...

Spring的对象拷贝BeanUtils

package gx.springboot.schedule.common.util; import org.springframework.beans.BeanUtils; import org.springframework.util.CollectionUtils; import java.util.ArrayList; import...

Java 的ArrayList构造方法

目录 ArrayList 构造方法使用 ArrayList 类常用方法 ArrayList集合存储字符串遍历 简单的学生管理系统   1、ArrayList 构造方法 ArrayList类概述  什么是集合   提供一种存储空间可变的存储模型,存储的数据容量可以发生改变 ‘   ArrayList集合的特点 底层是数组实现的,长度可以变化   泛型的...

Java ArrayList【笔记】

Java ArrayList【笔记】 ArrayList ArrayList基本结构 ArrayList 整体架构比较简单,就是一个数组结构 源码中的基本概念 index 表示数组的下标,从 0 开始计数 elementData 表示数组本身 DEFAULT_CAPACITY 表示数组的初始大小,默认是 10 size 表示当前数组的大小,类型 int,...