尽管已同步,Vector仍会抛出ConcurrentModificationException

bri*_*akh 1 java multithreading synchronization vector concurrentmodification

我有一个由多个线程操作的ArrayList,由于ArrayList未同步,因此无法正常工作.我按照教授的指示将列表切换为Vector.向量是同步的,但我有与同步相关的异常抛出.

为什么会发生这种情况,如何在代码中避免并发异常?我不想只是玩游戏直到有效,我想做最好的事情.谢谢!

例外:

Exception in thread "Thread-3" java.util.ConcurrentModificationException
    at java.util.Vector$Itr.checkForComodification(Vector.java:1184)
    at java.util.Vector$Itr.next(Vector.java:1137)
    at BytePe4D$ReadInts.run(BytePe4D.java:64)
Run Code Online (Sandbox Code Playgroud)

码:

import java.io.*;
import java.util.Vector;

public class BytePe4D {
    private Vector<Integer> numbers;

    public static void main(String[] args) {
        new BytePe4D();
    }

    public BytePe4D() {
        // Create ArrayList and reset sum
        numbers = new Vector<Integer>();

        // Call addInts 8 times, with filenames integer1.dat through integer8.dat
        for (int i = 1; i <= 8; i++) {
            File file = new File("PE Data/integer" + i + ".dat");
            ReadInts thread = new ReadInts(file);
            thread.start();
        }
    }

    /** Represents a Thread instance */
    class ReadInts extends Thread {
        File file;

        public ReadInts(File _file) {
            file = _file;
        }

        @Override
        public void run() {
            int count = 0;  // track number of records read
            int sum = 0;

            try {
                // Open stream to binary data file integer1.dat
                FileInputStream in = new FileInputStream(file);
                // Buffer the stream
                BufferedInputStream bin = new BufferedInputStream(in);
                // Access the primitive data
                DataInputStream din = new DataInputStream(bin);

                try {
                    // Read file until end reached
                    while (true) {
                        numbers.add(din.readInt());
                        count++;
                    }
                } catch (EOFException eof) {
                    // System.out.println("End of file reached.");
                } finally {
                    // Close streams
                    din.close();
                }
            } catch (FileNotFoundException fnf) {
                System.out.println("File does not exist: " + file.getName());
                return;
            } catch (IOException ioe) {
                ioe.printStackTrace();
            }

            // Calculate sum of numbers read
            for (int num : numbers) {
                sum += num;
            }

            // Write info
            System.out.println(
                String.format("%s%s%-5s%s%-8d%-5s%s%-12d%-5s%s%d",
                "Filename = ", file.getName(), "",
                "Count = ", count, "",
                "Sum = ", sum, "",
                "In List = ", numbers.size()));
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

xia*_*.li 5

你的代码似乎错了.

如果每个线程都要计算单个文件中的记录总和,我不明白为什么需要共享向量.另一方面,如果要计算所有文件中的记录总和,则应在每个线程完成后执行此操作.

根据您愿意,你可以:1)为每个线程创建一个矢量并计算总和在主线程中的每个文件,或2),等待所有线程完成再计算总和的所有文件.