0

Error "java.util.ConcurrentModificationException" sometimes appear, when I test more than three times the error will show up, I do not understand why it is always pointing to the "Word w = iter.next ();" and why it does not always show error.

//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");
        }

    }

// error

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)

Thanks!

Tan Nguyen
  • 65
  • 1
  • 8

3 Answers3

2

Java Collection classes are fail-fast which means that if the Collection will be changed while some thread is traversing over it using iterator, the iterator.next() will throw a ConcurrentModificationException.

While one thread is iterating over lw (in showWord method), another thread is trying to add a Word in lw.

Amit Dalal
  • 642
  • 8
  • 20
2

In multi Threaded environment while multiple threads are accessing same List it is safe to use CopyOnWriteArrayList.

Java 5 improves on the synchronized collections by providing several concurrent collection classes in java.util.concurrent package.

Also here is stackAnswer.

Community
  • 1
  • 1
rns
  • 1,047
  • 10
  • 25
1

Is lw an ArrayList? if so when multiple threads access it at the same time it will throw the java.util.ConcurrentModificationException since it is not thread safe. You should use java.util.Vector instead as it is synchronized.

Abe
  • 8,623
  • 10
  • 50
  • 74