java中的fail-fast(快速失败)机制

本文转载https://blog.csdn.net/zymx14/article/details/78394464

java中的fail-fast(快速失败)机制

引入

在前面介绍 ArrayList的扩容问题时对于modCount的操作没有详细说明,该变量的操作在add,remove等操作中都会发生改变。那么该变量到底有什么作用呢?

简介

fail-fast 机制,即快速失败机制,是java集合(Collection)中的一种错误检测机制。当在迭代集合的过程中该集合在结构上发生改变的时候,就有可能会发生fail-fast,即抛出 ConcurrentModificationException异常。fail-fast机制并不保证在不同步的修改下一定会抛出异常,它只是尽最大努力去抛出,所以这种机制一般仅用于检测bug。

fail-fast的出现场景

在我们常见的java集合中就可能出现fail-fast机制,比如ArrayList,HashMap。在多线程和单线程环境下都有可能出现快速失败。

1、单线程环境下的fail-fast

ArrayList发生fail-fast例子:

 
  1. public static void main(String[] args) {

  2. List<String> list = new ArrayList<>();

  3. for (int i = 0 ; i < 10 ; i++ ) {

  4. list.add(i + "");

  5. }

  6. Iterator<String> iterator = list.iterator();

  7. int i = 0 ;

  8. while(iterator.hasNext()) {

  9. if (i == 3) {

  10. list.remove(3);

  11. }

  12. System.out.println(iterator.next());

  13. i ++;

  14. }

  15. }

该段代码定义了一个Arraylist集合,并使用迭代器遍历,在遍历过程中,刻意在某一步迭代中remove一个元素,这个时候,就会发生fail-fast。

HashMap发生fail-fast:

  1. public static void main(String[] args) {

  2. Map<String, String> map = new HashMap<>();

  3. for (int i = 0 ; i < 10 ; i ++ ) {

  4. map.put(i+"", i+"");

  5. }

  6. Iterator<Entry<String, String>> it = map.entrySet().iterator();

  7. int i = 0;

  8. while (it.hasNext()) {

  9. if (i == 3) {

  10. map.remove(3+"");

  11. }

  12. Entry<String, String> entry = it.next();

  13. System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());

  14. i++;

  15. }

  16. }

该段代码定义了一个hashmap对象并存放了10个键值对,在迭代遍历过程中,使用map的remove方法移除了一个元素,导致抛出了 ConcurrentModificationException异常:

2、多线程环境下:

 
  1. public class FailFastTest {

  2. public static List<String> list = new ArrayList<>();

  3.  
  4. private static class MyThread1 extends Thread {

  5. @Override

  6. public void run() {

  7. Iterator<String> iterator = list.iterator();

  8. while(iterator.hasNext()) {

  9. String s = iterator.next();

  10. System.out.println(this.getName() + ":" + s);

  11. try {

  12. Thread.sleep(1000);

  13. } catch (InterruptedException e) {

  14. e.printStackTrace();

  15. }

  16. }

  17. super.run();

  18. }

  19. }

  20.  
  21. private static class MyThread2 extends Thread {

  22. int i = 0;

  23. @Override

  24. public void run() {

  25. while (i < 10) {

  26. System.out.println("thread2:" + i);

  27. if (i == 2) {

  28. list.remove(i);

  29. }

  30. try {

  31. Thread.sleep(1000);

  32. } catch (InterruptedException e) {

  33. e.printStackTrace();

  34. }

  35. i ++;

  36. }

  37. }

  38. }

  39.  
  40. public static void main(String[] args) {

  41. for(int i = 0 ; i < 10;i++){

  42. list.add(i+"");

  43. }

  44. MyThread1 thread1 = new MyThread1();

  45. MyThread2 thread2 = new MyThread2();

  46. thread1.setName("thread1");

  47. thread2.setName("thread2");

  48. thread1.start();

  49. thread2.start();

  50. }

  51. }

启动两个线程,分别对其中一个对list进行迭代,另一个在线程1的迭代过程中去remove一个元素,结果也是抛出了java.util.ConcurrentModificationException

fail-fast的原理

fail-fast是如何抛出ConcurrentModificationException异常的,又是在什么情况下才会抛出?

我们知道,对于集合如list,map类,我们都可以通过迭代器来遍历,而Iterator其实只是一个接口,具体的实现还是要看具体的集合类中的内部类去实现Iterator并实现相关方法。这里我们就以ArrayList类为例。在ArrayList中,当调用list.iterator()时,其源码是:

 
  1. public Iterator<E> iterator() {

  2. return new Itr();

  3. }

即它会返回一个新的Itr类,而Itr类是ArrayList的内部类,实现了Iterator接口,下面是该类的源码:

 
  1. /**

  2. * An optimized version of AbstractList.Itr

  3. */

  4. private class Itr implements Iterator<E> {

  5. int cursor; // index of next element to return

  6. int lastRet = -1; // index of last element returned; -1 if no such

  7. int expectedModCount = modCount;

  8.  
  9. public boolean hasNext() {

  10. return cursor != size;

  11. }

  12.  
  13. @SuppressWarnings("unchecked")

  14. public E next() {

  15. checkForComodification();

  16. int i = cursor;

  17. if (i >= size)

  18. throw new NoSuchElementException();

  19. Object[] elementData = ArrayList.this.elementData;

  20. if (i >= elementData.length)

  21. throw new ConcurrentModificationException();

  22. cursor = i + 1;

  23. return (E) elementData[lastRet = i];

  24. }

  25.  
  26. public void remove() {

  27. if (lastRet < 0)

  28. throw new IllegalStateException();

  29. checkForComodification();

  30.  
  31. try {

  32. ArrayList.this.remove(lastRet);

  33. cursor = lastRet;

  34. lastRet = -1;

  35. expectedModCount = modCount;

  36. } catch (IndexOutOfBoundsException ex) {

  37. throw new ConcurrentModificationException();

  38. }

  39. }

  40.  
  41. @Override

  42. @SuppressWarnings("unchecked")

  43. public void forEachRemaining(Consumer<? super E> consumer) {

  44. Objects.requireNonNull(consumer);

  45. final int size = ArrayList.this.size;

  46. int i = cursor;

  47. if (i >= size) {

  48. return;

  49. }

  50. final Object[] elementData = ArrayList.this.elementData;

  51. if (i >= elementData.length) {

  52. throw new ConcurrentModificationException();

  53. }

  54. while (i != size && modCount == expectedModCount) {

  55. consumer.accept((E) elementData[i++]);

  56. }

  57. // update once at end of iteration to reduce heap write traffic

  58. cursor = i;

  59. lastRet = i - 1;

  60. checkForComodification();

  61. }

  62.  
  63. final void checkForComodification() {

  64. if (modCount != expectedModCount)

  65. throw new ConcurrentModificationException();

  66. }

  67. }

其中,有三个属性:

 
  1. int cursor; // index of next element to return

  2. int lastRet = -1; // index of last element returned; -1 if no such

  3. int expectedModCount = modCount;

cursor是指集合遍历过程中的即将遍历的元素的索引,lastRet是cursor -1,默认为-1,即不存在上一个时,为-1,它主要用于记录刚刚遍历过的元素的索引。expectedModCount这个就是fail-fast判断的关键变量了,它初始值就为ArrayList中的modCount。(modCount是抽象类AbstractList中的变量,默认为0,而ArrayList 继承了AbstractList ,所以也有这个变量,modCount用于记录集合操作过程中作的修改次数,与size还是有区别的,并不一定等于size)

我们一步一步来看:

 
  1. public boolean hasNext() {

  2. return cursor != size;

  3. }

迭代器迭代结束的标志就是hasNext()返回false,而该方法就是用cursor游标和size(集合中的元素数目)进行对比,当cursor等于size时,表示已经遍历完成。

接下来看看最关心的next()方法,看看为什么在迭代过程中,如果有线程对集合结构做出改变,就会发生fail-fast:

 
  1. @SuppressWarnings("unchecked")

  2. public E next() {

  3. checkForComodification();

  4. int i = cursor;

  5. if (i >= size)

  6. throw new NoSuchElementException();

  7. Object[] elementData = ArrayList.this.elementData;

  8. if (i >= elementData.length)

  9. throw new ConcurrentModificationException();

  10. cursor = i + 1;

  11. return (E) elementData[lastRet = i];

  12. }

从源码知道,每次调用next()方法,在实际访问元素前,都会调用checkForComodification方法,该方法源码如下:

 
  1. final void checkForComodification() {

  2. if (modCount != expectedModCount)

  3. throw new ConcurrentModificationException();

  4. }

可以看出,该方法才是判断是否抛出ConcurrentModificationException异常的关键。在该段代码中,当modCount != expectedModCount

时,就会抛出该异常。但是在一开始的时候,expectedModCount初始值默认等于modCount,为什么会出现modCount != expectedModCount,很明显expectedModCount在整个迭代过程除了一开始赋予初始值modCount外,并没有再发生改变,所以可能发生改变的就只有modCount,在前面关于ArrayList扩容机制的分析中,可以知道在ArrayList进行add,remove,clear等涉及到修改集合中的元素个数的操作时,modCount就会发生改变(modCount ++),所以当另一个线程(并发修改)或者同一个线程遍历过程中,调用相关方法使集合的个数发生改变,就会使modCount发生变化,这样在checkForComodification方法中就会抛出ConcurrentModificationException异常。

类似的,hashMap中发生的原理也是一样的。

避免fail-fast

了解了fail-fast机制的产生原理,接下来就看看如何解决fail-fast

方法1

在单线程的遍历过程中,如果要进行remove操作,可以调用迭代器的remove方法而不是集合类的remove方法。看看ArrayList中迭代器的remove方法的源码:

 
  1. public void remove() {

  2. if (lastRet < 0)

  3. throw new IllegalStateException();

  4. checkForComodification();

  5.  
  6. try {

  7. ArrayList.this.remove(lastRet);

  8. cursor = lastRet;

  9. lastRet = -1;

  10. expectedModCount = modCount;

  11. } catch (IndexOutOfBoundsException ex) {

  12. throw new ConcurrentModificationException();

  13. }

  14. }

 

可以看到,该remove方法并不会修改modCount的值,并且不会对后面的遍历造成影响,因为该方法remove不能指定元素,只能remove当前遍历过的那个元素,所以调用该方法并不会发生fail-fast现象。该方法有局限性。

例子:

 
  1. public static void main(String[] args) {

  2. List<String> list = new ArrayList<>();

  3. for (int i = 0 ; i < 10 ; i++ ) {

  4. list.add(i + "");

  5. }

  6. Iterator<String> iterator = list.iterator();

  7. int i = 0 ;

  8. while(iterator.hasNext()) {

  9. if (i == 3) {

  10. iterator.remove(); //迭代器的remove()方法

  11. }

  12. System.out.println(iterator.next());

  13. i ++;

  14. }

  15. }

方法2

使用java并发包(java.util.concurrent)中的类来代替 ArrayList 和hashMap。

比如使用 CopyOnWriterArrayList代替 ArrayList, CopyOnWriterArrayList在是使用上跟 ArrayList几乎一样, CopyOnWriter是写时复制的容器(COW),在读写时是线程安全的。该容器在对add和remove等操作时,并不是在原数组上进行修改,而是将原数组拷贝一份,在新数组上进行修改,待完成后,才将指向旧数组的引用指向新数组,所以对于 CopyOnWriterArrayList在迭代过程并不会发生fail-fast现象。但 CopyOnWrite容器只能保证数据的最终一致性,不能保证数据的实时一致性。

对于HashMap,可以使用ConcurrentHashMap, ConcurrentHashMap采用了锁机制,是线程安全的。在迭代方面,ConcurrentHashMap使用了一种不同的迭代方式。在这种迭代方式中,当iterator被创建后集合再发生改变就不再是抛出ConcurrentModificationException,取而代之的是在改变时new新的hashEntry,从而不影响原有的数据 ,iterator完成后再将头指针替换为新的hashEntry ,这样iterator线程可以使用原来老的数据,而写线程也可以并发的完成改变。即迭代不会发生fail-fast,但不保证获取的是最新的数据。

参考链接:

http://www.jb51.net/article/84468.htm

http://www.cnblogs.com/ccgjava/p/6347425.html?utm_source=itdadao&utm_medium=referral

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值