转载自   Java 集合系列04之 fail-fast总结

概要

前面,我们已经学习了ArrayList。接下来,我们以ArrayList为例,对Iterator的fail-fast机制进行了解。内容包括::
1 fail-fast简介
2 fail-fast示例
3 fail-fast解决办法
4 fail-fast原理
5 解决fail-fast的原理

1 fail-fast简介

fail-fast 机制是java集合(Collection)中的一种错误机制。当多个线程对同一个集合的内容进行操作时,就可能会产生fail-fast事件。
例如:当某一个线程A通过iterator去遍历某集合的过程中,若该集合的内容被其他线程所改变了;那么线程A访问集合时,就会抛出ConcurrentModificationException异常,产生fail-fast事件。

在详细介绍fail-fast机制的原理之前,先通过一个示例来认识fail-fast。

2 fail-fast示例

示例代码:(FastFailTest.java)

import java.util.*;
import java.util.concurrent.*;/** @desc java集合中Fast-Fail的测试程序。**   fast-fail事件产生的条件:当多个线程对Collection进行操作时,若其中某一个线程通过iterator去遍历集合时,该集合的内容被其他线程所改变;则会抛出ConcurrentModificationException异常。*   fast-fail解决办法:通过util.concurrent集合包下的相应类去处理,则不会产生fast-fail事件。**   本例中,分别测试ArrayList和CopyOnWriteArrayList这两种情况。ArrayList会产生fast-fail事件,而CopyOnWriteArrayList不会产生fast-fail事件。*   (01) 使用ArrayList时,会产生fast-fail事件,抛出ConcurrentModificationException异常;定义如下:*            private static List<String> list = new ArrayList<String>();*   (02) 使用时CopyOnWriteArrayList,不会产生fast-fail事件;定义如下:*            private static List<String> list = new CopyOnWriteArrayList<String>();** @author skywang*/
public class FastFailTest {private static List<String> list = new ArrayList<String>();//private static List<String> list = new CopyOnWriteArrayList<String>();public static void main(String[] args) {// 同时启动两个线程对list进行操作!new ThreadOne().start();new ThreadTwo().start();}private static void printAll() {System.out.println("");String value = null;Iterator iter = list.iterator();while(iter.hasNext()) {value = (String)iter.next();System.out.print(value+", ");}}/*** 向list中依次添加0,1,2,3,4,5,每添加一个数之后,就通过printAll()遍历整个list*/private static class ThreadOne extends Thread {public void run() {int i = 0;while (i<6) {list.add(String.valueOf(i));printAll();i++;}}}/*** 向list中依次添加10,11,12,13,14,15,每添加一个数之后,就通过printAll()遍历整个list*/private static class ThreadTwo extends Thread {public void run() {int i = 10;while (i<16) {list.add(String.valueOf(i));printAll();i++;}}}}

运行结果
运行该代码,抛出异常java.util.ConcurrentModificationException!即,产生fail-fast事件!

结果说明
(01) FastFailTest中通过 new ThreadOne().start() 和 new ThreadTwo().start() 同时启动两个线程去操作list。
    ThreadOne线程:向list中依次添加0,1,2,3,4,5。每添加一个数之后,就通过printAll()遍历整个list。
    ThreadTwo线程:向list中依次添加10,11,12,13,14,15。每添加一个数之后,就通过printAll()遍历整个list。
(02) 当某一个线程遍历list的过程中,list的内容被另外一个线程所改变了;就会抛出ConcurrentModificationException异常,产生fail-fast事件。

3 fail-fast解决办法

fail-fast机制,是一种错误检测机制。它只能被用来检测错误,因为JDK并不保证fail-fast机制一定会发生。若在多线程环境下使用fail-fast机制的集合,建议使用“java.util.concurrent包下的类”去取代“java.util包下的类”。
所以,本例中只需要将ArrayList替换成java.util.concurrent包下对应的类即可。

即,将代码

private static List<String> list = new ArrayList<String>();

替换为

private static List<String> list = new CopyOnWriteArrayList<String>();

则可以解决该办法。

4 fail-fast原理

产生fail-fast事件,是通过抛出ConcurrentModificationException异常来触发的。
那么,ArrayList是如何抛出ConcurrentModificationException异常的呢?

我们知道,ConcurrentModificationException是在操作Iterator时抛出的异常。我们先看看Iterator的源码。ArrayList的Iterator是在父类AbstractList.java中实现的。代码如下:

package java.util;public abstract class AbstractList<E> extends AbstractCollection<E> implements List<E> {...// AbstractList中唯一的属性// 用来记录List修改的次数:每修改一次(添加/删除等操作),将modCount+1protected transient int modCount = 0;// 返回List对应迭代器。实际上,是返回Itr对象。public Iterator<E> iterator() {return new Itr();}// Itr是Iterator(迭代器)的实现类private class Itr implements Iterator<E> {int cursor = 0;int lastRet = -1;// 修改数的记录值。// 每次新建Itr()对象时,都会保存新建该对象时对应的modCount;// 以后每次遍历List中的元素的时候,都会比较expectedModCount和modCount是否相等;// 若不相等,则抛出ConcurrentModificationException异常,产生fail-fast事件。int expectedModCount = modCount;public boolean hasNext() {return cursor != size();}public E next() {// 获取下一个元素之前,都会判断“新建Itr对象时保存的modCount”和“当前的modCount”是否相等;// 若不相等,则抛出ConcurrentModificationException异常,产生fail-fast事件。checkForComodification();try {E next = get(cursor);lastRet = cursor++;return next;} catch (IndexOutOfBoundsException e) {checkForComodification();throw new NoSuchElementException();}}public void remove() {if (lastRet == -1)throw new IllegalStateException();checkForComodification();try {AbstractList.this.remove(lastRet);if (lastRet < cursor)cursor--;lastRet = -1;expectedModCount = modCount;} catch (IndexOutOfBoundsException e) {throw new ConcurrentModificationException();}}final void checkForComodification() {if (modCount != expectedModCount)throw new ConcurrentModificationException();}}...
}

从中,我们可以发现在调用 next() 和 remove()时,都会执行 checkForComodification()。若 “modCount 不等于 expectedModCount”,则抛出ConcurrentModificationException异常,产生fail-fast事件。

要搞明白 fail-fast机制,我们就要需要理解什么时候“modCount 不等于 expectedModCount”!
从Itr类中,我们知道 expectedModCount 在创建Itr对象时,被赋值为 modCount。通过Itr,我们知道:expectedModCount不可能被修改为不等于 modCount。所以,需要考证的就是modCount何时会被修改。

接下来,我们查看ArrayList的源码,来看看modCount是如何被修改的。

package java.util;public class ArrayList<E> extends AbstractList<E>implements List<E>, RandomAccess, Cloneable, java.io.Serializable
{...// list中容量变化时,对应的同步函数public void ensureCapacity(int minCapacity) {modCount++;int oldCapacity = elementData.length;if (minCapacity > oldCapacity) {Object oldData[] = elementData;int newCapacity = (oldCapacity * 3)/2 + 1;if (newCapacity < minCapacity)newCapacity = minCapacity;// minCapacity is usually close to size, so this is a win:elementData = Arrays.copyOf(elementData, newCapacity);}}// 添加元素到队列最后public boolean add(E e) {// 修改modCountensureCapacity(size + 1);  // Increments modCount!!elementData[size++] = e;return true;}// 添加元素到指定的位置public void add(int index, E element) {if (index > size || index < 0)throw new IndexOutOfBoundsException("Index: "+index+", Size: "+size);// 修改modCountensureCapacity(size+1);  // Increments modCount!!System.arraycopy(elementData, index, elementData, index + 1,size - index);elementData[index] = element;size++;}// 添加集合public boolean addAll(Collection<? extends E> c) {Object[] a = c.toArray();int numNew = a.length;// 修改modCountensureCapacity(size + numNew);  // Increments modCountSystem.arraycopy(a, 0, elementData, size, numNew);size += numNew;return numNew != 0;}// 删除指定位置的元素 public E remove(int index) {RangeCheck(index);// 修改modCountmodCount++;E oldValue = (E) elementData[index];int numMoved = size - index - 1;if (numMoved > 0)System.arraycopy(elementData, index+1, elementData, index, numMoved);elementData[--size] = null; // Let gc do its workreturn oldValue;}// 快速删除指定位置的元素 private void fastRemove(int index) {// 修改modCountmodCount++;int numMoved = size - index - 1;if (numMoved > 0)System.arraycopy(elementData, index+1, elementData, index,numMoved);elementData[--size] = null; // Let gc do its work}// 清空集合public void clear() {// 修改modCountmodCount++;// Let gc do its workfor (int i = 0; i < size; i++)elementData[i] = null;size = 0;}...
}

从中,我们发现:无论是add()、remove(),还是clear(),只要涉及到修改集合中的元素个数时,都会改变modCount的值。

接下来,我们再系统的梳理一下fail-fast是怎么产生的。步骤如下:
(01) 新建了一个ArrayList,名称为arrayList。
(02) 向arrayList中添加内容。
(03) 新建一个“线程a”,并在“线程a”中通过Iterator反复的读取arrayList的值
(04) 新建一个“线程b”,在“线程b”中删除arrayList中的一个“节点A”。
(05) 这时,就会产生有趣的事件了。
       在某一时刻,“线程a”创建了arrayList的Iterator。此时“节点A”仍然存在于arrayList中,创建arrayList时,expectedModCount = modCount(假设它们此时的值为N)。
       在“线程a”在遍历arrayList过程中的某一时刻,“线程b”执行了,并且“线程b”删除了arrayList中的“节点A”。“线程b”执行remove()进行删除操作时,在remove()中执行了“modCount++”,此时modCount变成了N+1
“线程a”接着遍历,当它执行到next()函数时,调用checkForComodification()比较“expectedModCount”和“modCount”的大小;而“expectedModCount=N”,“modCount=N+1”,这样,便抛出ConcurrentModificationException异常,产生fail-fast事件。

至此,我们就完全了解了fail-fast是如何产生的!
即,当多个线程对同一个集合进行操作的时候,某线程访问集合的过程中,该集合的内容被其他线程所改变(即其它线程通过add、remove、clear等方法,改变了modCount的值);这时,就会抛出ConcurrentModificationException异常,产生fail-fast事件。

5 解决fail-fast的原理

上面,说明了“解决fail-fast机制的办法”,也知道了“fail-fast产生的根本原因”。接下来,我们再进一步谈谈java.util.concurrent包中是如何解决fail-fast事件的。

还是以和ArrayList对应的CopyOnWriteArrayList进行说明。我们先看看CopyOnWriteArrayList的源码:

package java.util.concurrent;
import java.util.*;
import java.util.concurrent.locks.*;
import sun.misc.Unsafe;public class CopyOnWriteArrayList<E>implements List<E>, RandomAccess, Cloneable, java.io.Serializable {...// 返回集合对应的迭代器public Iterator<E> iterator() {return new COWIterator<E>(getArray(), 0);}...private static class COWIterator<E> implements ListIterator<E> {private final Object[] snapshot;private int cursor;private COWIterator(Object[] elements, int initialCursor) {cursor = initialCursor;// 新建COWIterator时,将集合中的元素保存到一个新的拷贝数组中。// 这样,当原始集合的数据改变,拷贝数据中的值也不会变化。snapshot = elements;}public boolean hasNext() {return cursor < snapshot.length;}public boolean hasPrevious() {return cursor > 0;}public E next() {if (! hasNext())throw new NoSuchElementException();return (E) snapshot[cursor++];}public E previous() {if (! hasPrevious())throw new NoSuchElementException();return (E) snapshot[--cursor];}public int nextIndex() {return cursor;}public int previousIndex() {return cursor-1;}public void remove() {throw new UnsupportedOperationException();}public void set(E e) {throw new UnsupportedOperationException();}public void add(E e) {throw new UnsupportedOperationException();}}...}

从中,我们可以看出:

(01) 和ArrayList继承于AbstractList不同,CopyOnWriteArrayList没有继承于AbstractList,它仅仅只是实现了List接口。
(02) ArrayList的iterator()函数返回的Iterator是在AbstractList中实现的;而CopyOnWriteArrayList是自己实现Iterator。
(03) ArrayList的Iterator实现类中调用next()时,会“调用checkForComodification()比较‘expectedModCount’和‘modCount’的大小”;但是,CopyOnWriteArrayList的Iterator实现类中,没有所谓的checkForComodification(),更不会抛出ConcurrentModificationException异常!

Java 集合系列04之 fail-fast总结相关推荐

  1. Java 集合系列04之 fail-fast总结(通过ArrayList来说明fail-fast的原理、解决办法)

    http://www.cnblogs.com/skywang12345/p/3308762.html 概要 前面,我们已经学习了ArrayList.接下来,我们以ArrayList为例,对Iterat ...

  2. Java 集合系列目录(Category)

    Java 集合系列目录(Category) 转自:Java 集合系列目录(Category) 01. Java 集合系列01之 总体框架 02. Java 集合系列02之 Collection架构 0 ...

  3. Java 集合系列 16 HashSet

    java 集合系列目录: Java 集合系列 01 总体框架 Java 集合系列 02 Collection架构 Java 集合系列 03 ArrayList详细介绍(源码解析)和使用示例 Java ...

  4. 新手菜鸟 Java 集合系列目录(Category)

    Java 集合系列01之 总体框架 http://www.cnblogs.com/skywang12345/p/3308498.html (笔记:Java集合是java提供的工具包,包含了常用的数据结 ...

  5. Java 集合系列06: Vector深入解析

    戳上面的蓝字关注我们哦! 精彩内容 精选java等全套视频教程 精选java电子图书 大数据视频教程精选 java项目练习精选 概论 这是接着以前的文章分享的,这里给出以前的文章的连接,供小伙伴们回顾 ...

  6. Java 集合系列03之 ArrayList详细介绍(源码解析)和使用示例

    转载自  Java 集合系列03之 ArrayList详细介绍(源码解析)和使用示例 第1部分 ArrayList介绍 ArrayList简介 ArrayList 是一个数组队列,相当于 动态数组.与 ...

  7. Java 集合系列02之 Collection架构

    概要 首先,我们对Collection进行说明.下面先看看Collection的一些框架类的关系图: Java 集合系列02之 Collection架构 Collection是一个接口,它主要的两个分 ...

  8. Java 集合系列(一)

    Java集合系列文章将以思维导图为主要形式来展示知识点,让零碎的知识形成体系. 这篇文章主要介绍的是[Java 集合的基本知识],即Java 集合简介. 毕业出来一直使用 PHP 进行开发,对于大学所 ...

  9. 深入Java集合系列之五:PriorityQueue

    转载自  深入Java集合系列之五:PriorityQueue 前言 今天继续来分析一下PriorityQueue的源码实现,实际上在Java集合框架中,还有ArrayDeque(一种双端队列),这里 ...

最新文章

  1. 灰暗而空虚的景色β(数学思维题)
  2. NLP中各框架对变长序列的处理全解
  3. Ollydbg使用教程学习总结(五)
  4. ASP.NET知识集
  5. 雨林木风win11 64位全新专业版系统v2021.08
  6. 怎么成为开源贡献者_成为负责任的开源用户
  7. ios签名软件_使用ios企业签名需要准备哪些?
  8. 第102天:CSS3实现立方体旋转
  9. 计算机三级信息安全技术试题与答案,计算机三级《信息安全技术》练习题与答案...
  10. Jackson 学习笔记 XML转JSON
  11. stackoverflow 瞎眼 模式,你中招了么?
  12. 惊呆了,Java居然已经内卷成菜花啦。
  13. writing idiomatic python 读书笔记(2)
  14. NCCL配置多卡运行
  15. 计算机专业专科可以进的国企,专科毕业生想进国企?这三大专业不要错过,成功率高达60%...
  16. 你可以忍受大城市 365 天的孤独,却不能忍受小城市 7 天的热闹
  17. 天下数据支招如何防范域名被劫持
  18. RK3288 Android7.1 接USB摄像头后扬声器无声音
  19. LINUX-配置环境变量
  20. boj489. 小妹妹去划船

热门文章

  1. 7-3 树的同构 (25 分)(思路加详解)来呀baby!!!!!!!!
  2. Java多线程之龟兔赛跑和抢票
  3. xml可以html标签吗,自定义html标签(XML)
  4. array remove java_how to remove array from another array in javascript
  5. JAVA开发需求分析套路_毕设做什么好?感觉都是套路了
  6. Mysql@和@@符号的详细使用说明
  7. [高等数学]这你不背?
  8. 《C++ Primer》13.1.4节练习
  9. Almost Union-Find UVA - 11987(并查集的删除操作)
  10. Harbour.Space Scholarship Contest 2021-2022 F. Pairwise Modulo 逆向思维 + 树状数组