package com.test.io;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.LinkedList;

import com.mysql.jdbc.Buffer;

public class ReadKeyFile {
    public static void main(String[] args) throws IOException {

        BufferedReader bufferedReader=null;
        try {
            bufferedReader = new BufferedReader(new FileReader("C:/Users/Administrator/Desktop/test.txt"));
            LinkedList<String> list = new LinkedList<String>();
            String str=bufferedReader.readLine();
            while((str=bufferedReader.readLine())!=null) {
                System.out.println(str);
                list.add(str);
            }
            System.out.println("------------");
            for (String string : list) {
                System.out.println(list.removeLast());
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(bufferedReader != null)
                bufferedReader.close();
        }
    }
}

结果及报错:

22222222222222222222
33333333333333333333
44444444444444444444
55555555555555555555
66666666666666666666
77777777777777777777
------------
77777777777777777777
java.util.ConcurrentModificationException
    at java.util.LinkedList$ListItr.checkForComodification(LinkedList.java:966)
    at java.util.LinkedList$ListItr.next(LinkedList.java:888)
    at com.test.io.ReadKeyFile.main(ReadKeyFile.java:24)

test.txt:

11111111111111111111
22222222222222222222
33333333333333333333
44444444444444444444
55555555555555555555
66666666666666666666
77777777777777777777

如果将源代码红色部分替换为以下,则可以正常输出,可见问题并不在remveLast()方法,而在迭代。

System.out.println(list.removeLast());
System.out.println(list.removeLast());

在网上查到一段文字:

1.在使用增强for循环进行集合的迭代的时候其实默认使用的是迭代器;2.因此在循环中不能使用集合的引用变量直接操作集合,避免导致多线程并发访问的安全性异常。

1的意思即增强for循环的遍历方式类似与:

Iterator<String> iterator = list.iterator();
            while(iterator.hasNext())
                System.out.println(iterator.next());

鉴于此,有必要看一下LinkedList的代码。在LinkedList中,这个iterator就是ListItr ,它是继承自Itr类,next()方法正是出自Itr类,在里面找到next()的源码如下:

public E next() {
            checkForComodification();
            try {
                int i = cursor;
                E next = get(i);
                lastRet = i;
                cursor = i + 1;
                return next;
            } catch (IndexOutOfBoundsException e) {
                checkForComodification();
                throw new NoSuchElementException();
            }
}

注意checkForComodification(),这就是报错的地方啦,具体是在下面这个地方报的错:

final void checkForComodification() {
            if (modCount != expectedModCount)
                throw new ConcurrentModificationException();
}

根据我们的测试:LinkedList打印出第一排7就报错了。

modCount 这个在打应出第一排7之后的值应该为8,因为add了7次,又removeLast了一次嘛

那expectedModCount为多少呢?

看看Itr源代码吧,真乱,又实在是不想去读注释,我还是去翻翻JAVA编程思想先。

好吧,翻了一下书没翻到,百度了一下,又重新看了下代码,继续上面的问题,expectedModCount的值。

expectedModCount的值在removeLast之后为7。因为add里面有expectedModCount=modCount这样的语句,而removeLast里面没有。

这样的机制,应该是为了检测一边迭代,一边修改集合的危险操作。

LinkedList - java.util.ConcurrentModificationException的更多相关文章

  1. Iterator之java.util.ConcurrentModificationException

    在运行以下代码时,会报java.util.ConcurrentModificationException异常, public class Demo { public static void main( ...

  2. 增强for循环 java.util.ConcurrentModificationException

    Java中的Iterator功能比较简单,并且只能单向移动: (1) 使用方法iterator()要求容器返回一个Iterator.第一次调用Iterator的next()方法时,它返回序列的第一个元 ...

  3. java.util.ConcurrentModificationException 解决办法(转载)

    今天在项目的中有一个需求,需要在一个Set类型的集合中删除满足条件的对象,这时想当然地想到直接调用Set的remove(Object o)方法将指定的对象删除即可,测试代码:   public cla ...

  4. java.util.ConcurrentModificationException --map

    key:3-key key:/v1.02-key Exception in thread "main" java.util.ConcurrentModificationExcept ...

  5. 偶遇到 java.util.ConcurrentModificationException 的异常

    今天在调试程序 遇到了如此问题 贴上代码来看看稍后分析 List<String> list = null;boolean isUpdate = false;try { list = JSO ...

  6. 对ArrayList操作时报错java.util.ConcurrentModificationException null

    用iterator遍历集合时要注意的地方:不可以对iterator相关的地方做添加或删除操作.否则会报java.util.ConcurrentModificationException 例如如下代码: ...

  7. java.util.ConcurrentModificationException 解决办法

    在使用iterator.hasNext()操作迭代器的时候,如果此时迭代的对象发生改变,比如插入了新数据,或者有数据被删除. 则使用会报以下异常:Java.util.ConcurrentModific ...

  8. java.util.ConcurrentModificationException 解决办法(转)

    今天在项目的中有一个需求,需要在一个Set类型的集合中删除满足条件的对象,这时想当然地想到直接调用Set的remove(Object o)方法将指定的对象删除即可,测试代码:   public cla ...

  9. java集合--java.util.ConcurrentModificationException异常

    ConcurrentModificationException 异常:并发修改异常,当方法检测到对象的并发修改,但不允许这种修改时,抛出此异常.一个线程对collection集合迭代,另一个线程对Co ...

随机推荐

  1. ModelAndView的介绍

    ModelAndView的构造方法有7个.但是它们都是相通的.这里使用无参构造函数来举例说明如何构造ModelAndView实例. ModelAndView类别就如其名称所示,是代表了MVC Web程 ...

  2. PHP 面向对象:抽象类继承抽象类

    抽象类继承另外一个抽象类时,不用重写其中的抽象方法.抽象类中,不能重写抽象父类的抽象方法.这样的用法,可以理解为对抽象类的扩展. 下面的例子,演示了一个抽象类继承自另外一个抽象类时,不需要重写其中的抽 ...

  3. 【GoLang】golang 面向对象编程 & 面向接口编程

    005.面向对象&接口编程 1 面向函数编程 1.1 将数据作为参数传递到函数入参 1.2 对象与函数是分离的 2 面向对象编程 2.1 使用者看起来函数作为对象的属性而非参数 2.2 函数属 ...

  4. 【架构】linkerd:来自Twitter为微服务而生的开源RPC解决方案

    大家要如何以规模化方式运维微服务应用程序?实践当中会出现哪些问题,我们又该如何加以解决?在大规模与非预测性工作负载场景当中,我们需要满足哪些条件才能运行一款大型微服务应用程序,而又能够确保不必受到功能 ...

  5. java获取对象属性类型、属性名称、属性值

    /** * 根据属性名获取属性值 * */ private Object getFieldValueByName(String fieldName, Object o) { try { String ...

  6. ios NSNotificationCenter 收到通知后的执行线程

    https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Notifications/Articles/Thread ...

  7. iOS keyChain 的使用

    详细资料,请参看苹果官方文档Keychain Services Reference . ios中的keychain,用于保存用户的机密信息,对keychain的操作有4种,就是 增,删,改,查: Se ...

  8. Delphi中Format与FormatDateTime函数详解

    copy:http://hi.baidu.com/yunfanleo/blog/item/0c51d9cdbc0531550eb34558.html Format是一个很常用,却又似乎很烦的方法,本人 ...

  9. Python批量修改文件名-后缀

    LyncLynn用途: 批量修改文件格式,文件名后缀. #Version: V1.0 #Author:lynclynn #Description:Change the filename #Create ...

  10. codeforces 556C. Case of Matryoshkas 解题报告

    题目链接:http://codeforces.com/contest/556/problem/C 题目意思:有 n 个数(1,2,...,n)组成 k 条链.第 i 条链由 mi 个数组成.每一秒只可 ...