Fail-Fast 前奏
来源:互联网 发布:java面向对象编程继承 编辑:程序博客网 时间:2024/06/05 04:56
示例代码1
public static void main(String[] args) { String string = "a b c d e"; List<String> stringList = Arrays.asList(string.split(" ")); Iterator<String> iterator = stringList.iterator(); while (iterator.hasNext()) { if(iterator.next().equals("c")) { stringList.remove("c"); } }}
输出:
Exception in thread "main" java.lang.UnsupportedOperationException at java.util.AbstractList.remove(AbstractList.java:161) at java.util.AbstractList$Itr.remove(AbstractList.java:374) at java.util.AbstractCollection.remove(AbstractCollection.java:293) at DotThis.main(DotThis.java:75)
报不支持的操作异常,UnsupportedOperationException。
ArrayList.asList返回的List是固定大小的List,也就是说不可以对其进行add、remove操作。
源码如下:
//Returns a fixed-size list backed by the specified array.@SafeVarargs@SuppressWarnings("varargs")public static <T> List<T> asList(T... a) { return new ArrayList<>(a);}//该ArrayList类不是java.util包中的内容,而是Arrays类的内部类。private static class ArrayList<E> extends AbstractList<E> implements RandomAccess, java.io.Serializable{ private static final long serialVersionUID = -2764017481108945198L; private final E[] a; ArrayList(E[] array) { a = Objects.requireNonNull(array); } /*省略此处代码*/}
从上面代码的注释中可以看到是 “返回的是固定大小的List”。从源码实现上可以看到,private final E[] a; ,内部类ArrayList中的成员变量a,是final的。
示例代码二
public static void main(String[] args) { String string = "a b c d e"; List<String> arrList = Arrays.asList(string.split(" ")); List<String> stringList = new ArrayList<String>(arrList); Iterator<String> iterator = stringList.iterator(); while (iterator.hasNext()) { if(iterator.next().equals("c")) { stringList.remove("c"); } }}
输出:
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 DotThis.main(DotThis.java:76)
每个容器源码都会有modCount,modCount用来表示容器被结构性修改的次数。所谓结构性修改是指,改变了容器的size,或者在迭代过程中产生了不正确的结果。
代码中,在迭代到”c”时,stringList.remove(“c”); 执行了remove操作,对list造成了结构性修改,改变了list的size,modCount的值会加1。
这样当迭代到”d”时,发现expectedModCount与modCount不等了,因此抛ConcurrentModificationException异常了。
示例代码三
public static void main(String[] args) { String string = "a b c d e"; List<String> stringList1 = Arrays.asList(string.split(" ")); List<String> stringList = new ArrayList<String>(stringList1); System.out.println(stringList); Iterator<String> iterator = stringList.iterator(); while (iterator.hasNext()) { if(iterator.next().equals("c")) iterator.remove();//这里跟上例不同,上例为stringList.remove("c"); } } System.out.println(stringList);}
输出:
[a, b, c, d, e][a, b, d, e]
查看ArrayList#Itr#remove方法:
private class Itr implements Iterator<E> { public void remove() { if (lastRet < 0) throw new IllegalStateException(); checkForComodification(); try { ArrayList.this.remove(lastRet); cursor = lastRet; lastRet = -1; expectedModCount = modCount; } catch (IndexOutOfBoundsException ex) { throw new ConcurrentModificationException(); } } final void checkForComodification() { if (modCount != expectedModCount) throw new ConcurrentModificationException(); } /**省略此处代码**/ }
从上面代码可以看到,首先也判断了expectedModCount与modCount是否相等,但是这时还没有执行remove操作,因此不会抛异常。在执行了ArrayList.this.remove(lastRet); 之后,有一个关键的一行expectedModCount = modCount; 更新了expectedModCount的值。
阅读全文
0 0
- Fail-Fast 前奏
- fail-fast
- fail fast
- fail-fast
- Fail-Fast
- Fail safe vs Fail fast
- Fail Fast Vs Fail Safe
- fail-fast机制
- Iterator的fail-fast
- Fail-fast机制
- Fail Fast机制
- fail-fast机制
- Fail-Fast 机制
- fail-fast机制
- fail-fast机制
- fail-fast机制
- Java fast-fail机制
- fail-fast机制
- linux下GCC的安装
- 一个10分钟神经网络演讲
- [树状数组][codevs1081]线段树练习2
- Java 中字符串的处理
- JavaScript--选择排序、冒泡排序
- Fail-Fast 前奏
- hdu 6227 Rabbits
- Android、ios双平台车牌识别,无需联网
- 济南刷题冲刺 Day1 下午
- c语言大数加法
- Keras指定使用GPU运算
- Python高级编程-如何为元祖中的每个元素命名,提高程序可读性?
- 多源最短路(floyd)
- 并发编程(10)-queue