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. discuz内置常用CSS代码分析

    CSS多IE下兼容HACK写法 所有 IE浏览器适用:.ie_all .foo { ... } IE6 专用:.ie6 .foo { ... } IE7 专用:.ie7 .foo { ... } IE ...

  2. Jquery Ajax处理,服务端三种页面aspx,ashx,asmx的比较

    常规的Jquery Ajax 验证登录,主要有3种服务端页面相应 ,也就是 aspx,ashx,asmx即webserivice . 下面分别用3种方式来(aspx,ashx,asmx)做服务端来处理 ...

  3. Interleaving Positive and Negative Numbers

    Given an array with positive and negative integers. Re-range it to interleaving with positive and ne ...

  4. codecademy-command line-inputoutput

    What happens when you type this command? $ echo "Hello" Hello The echo command accepts the ...

  5. ios Swift 一些注意事项

    func test(one:NSString) -> NSString{ return "aaa" } func test(one:Int) -> NSString{ ...

  6. 配置IIS,Apache,PHP过程中遇到的一些问题

    下载了eclipse的最新版本,并且添加了PHP插件.为了支持多语言,决定采用UTF-8编码.但是在开发的过程中,发现代码的自动提示帮助信息显示的是乱码,PHP源文件及注释,均正常.在网上查了很多资料 ...

  7. FFPlay-noConsole-ver-20160409-snapshot

    ESC 退出 0 进度条开关 1 屏幕原始大小 2 屏幕1/2大小 3 屏幕1/3大小 4 屏幕1/4大小 S 下一帧 [ -2秒 ] +2秒 ; -1秒 ' +1秒 下一个帧 -> -5秒 F ...

  8. 使用Ajax上传图片到服务器(不刷新页面)

    有时候我们需要上传图片时不刷新页面,那么Ajax就是很好的东西哦.之前在网上找了很多的资料都不对,不是这里就是那里错,这是本人亲自测试了的哈,是没有问题的,若有不足之处希望指正.我用的.net,对了这 ...

  9. 常用邮箱的服务器(SMTP/POP3)地址和端口总结

    163.com: POP3服务器地址:pop.163.com(端口:110) SMTP服务器地址:smtp.163.com(端口:25) 126邮箱: POP3服务器地址:pop.126.com(端口 ...

  10. 前端性能优化(DOM篇)

    原文链接:https://segmentfault.com/a/1190000000490322 缓存DOM对象 JavaScript的DOM操作可以说是JavaScript最重要的功能,我们经常要根 ...