读取多个文件 - java.util.ConcurrentModificationException

时间:2015-03-23 02:02:48

标签: java multithreading

错误“java.util.ConcurrentModificationException”有时会出现,当我测试三次以上时会出现错误,我不明白为什么它总是指向“Word w = iter.next();”以及为什么它并不总是显示错误。

// LoadFile.java

@Override
    public void run() {
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new InputStreamReader(
                    new FileInputStream(this.fileName), "UTF-8"));
            String line = reader.readLine();
            while (line != null) {
                w = null;
                String[] result1 = line.split(":");
                w = new Word();
                w.setId(result1[0]);
                w.setWord(result1[1]);
                w.setMean(result1[2]);
                lw.add(w);
                line = reader.readLine();
            }
            reader.close();
        } catch (FileNotFoundException e) {
            System.err.println(e.getMessage());
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("Thread " + fileName + " exiting.");
        Gui.showWord(lw);
    }

    public void start() {
        System.out.println("Starting " + fileName);
        if (t == null) {
            t = new Thread(this, fileName);
            t.start();
        }
    }

// Gui.java

private void chooseFile() {

        fileChooser = new JFileChooser();
        fileChooser.setMultiSelectionEnabled(true);
        int returnVal = fileChooser.showOpenDialog(Gui.this);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            file = fileChooser.getSelectedFiles();
            for (int i = 0; i <= file.length - 1; i++) {
                System.out.println(file[i].getName()
                        + ".........................");
                lf = new LoadFile(file[i].getName());
                lf.start();
            }

        } else {
            textAreaWord.append("Operation Canceled \n" + "new Line\n");
        }
    }

    public static void showWord(List<Word> ls) {
        Iterator<Word> iter = ls.iterator();
        while (iter.hasNext()) {
            Word w = iter.next();
            textAreaWord.append(w.getWord() + "\n");
        }

    }

//错误

Exception in thread "hello.txt" Thread hello.txt exiting.
Thread hi.txt exiting.
java.util.ConcurrentModificationException
    at java.util.ArrayList$Itr.checkForComodification(Unknown Source)
    at java.util.ArrayList$Itr.next(Unknown Source)
    at dictionary.Gui.showWord(Gui.java:138)
    at dictionary.LoadFile.run(LoadFile.java:46)
    at java.lang.Thread.run(Unknown Source)

谢谢!

3 个答案:

答案 0 :(得分:2)

Java Collection类是快速失败的,这意味着如果在某个线程使用迭代器遍历它时将更改Collection,则iterator.next()将抛出ConcurrentModificationException

当一个线程在lw上迭代时(在showWord方法中),另一个线程正在尝试在Word中添加lw

答案 1 :(得分:2)

在多线程环境中,当多个线程访问同一个List时,使用CopyOnWriteArrayList是安全的。

Java 5通过在 java.util.concurrent 包中提供多个并发集合类来改进同步集合。

此处还有stackAnswer

答案 2 :(得分:1)

lwArrayList吗?如果是这样,当多个线程同时访问它时,它将抛出java.util.ConcurrentModificationException,因为它不是线程安全的。在同步时,您应该使用java.util.Vector