java - 字段超出范围java

标签 java multithreading arraylist

我在多线程应用程序方面遇到一些问题。我想从类似 CSV 的文件加载数据并将其存储在缓冲区中以供以后检索。

当线程完成时,动物列表似乎超出了范围(?),我不明白有关 java 线程的一些内容,非常感谢您的帮助。

调用方式:

ParseCSV parser = new ParseCSV(null);
EventQueue.invokeLater(parser);
System.err.println("printing!");

编辑 - 根据要求,这是失败的部分 - 此时 praser 缓冲区的内容为空。我认为如果我在解析器中使用 new 关键字,动物的内容将是持久的。

while(parser.hasNext()){
    System.err.println("#");
    System.err.println(parser.getNext().toString());
}

解析器类:

public class ParseCSV implements Runnable{

    private String path = null;
    private File file = null;
    private static Logger log = LogManager.getLogger(ParseCSV.class.getName());
    private volatile ArrayList<Animal> animals = new ArrayList<Animal>();
    private int pointer =0;



    public ParseCSV(String path) {
        animals = new ArrayList<Animal>(); //tried to reinitialize, didn't help this is where the problem occurs
        if(path == null){
            JFileChooser jfc = new JFileChooser();
            jfc.showOpenDialog(null);
            file = jfc.getSelectedFile();
            this.path = file.getAbsolutePath();
        }
        else {
            this.path = path;

        }
        log.debug("Initialized file parser for " + this.path);
    }

    @Override
    public void run() {
        log.debug("begining file parse");
        System.err.println("runnner");
        try {
            Scanner fileScan = new Scanner(this.file);
            while(fileScan.hasNextLine()){
                parseLine(fileScan.nextLine());
            }
            fileScan.close();
        } catch (FileNotFoundException e) {
            log.error("Exception occured: " + e.getMessage() + "\nstack:\n" + e.getStackTrace());
            System.err.println("Exception: " + e.getMessage());
            e.printStackTrace();
        }
    }

    private void parseLine(String nextLine) {
        Scanner lineScanner = new Scanner(nextLine);
        lineScanner.useDelimiter("\\s\\|\\s");
        if(lineScanner.hasNext()){
            Animal an =             new Animal(lineScanner.next(), //person
                    lineScanner.next(), //animal
                    Integer.parseInt(lineScanner.next()), //emp-number
                    lineScanner.next(), //date
                    Integer.parseInt(lineScanner.next()), //ani-number
                    lineScanner.next());
            animals.add(an);    //contract ID
            System.err.println(an.toString()); //prints correct result!
        }
        lineScanner.close();
    }

    public String getPath() {       return path;    }
    public void setPath(String path) {      this.path = path;   }

    public boolean hasNext(){
        System.err.println("size of data = " + animals.size());
        if(animals.size() == pointer || animals.isEmpty()) return false;
        else return true;
    }
    public Sale getNext(){
        if(animals.size() == pointer) return null;
        return animals.get(pointer++);

    }

}

编辑 - 添加注释以指出问题出现的位置

最佳答案

您需要等待解析器线程完成(当前两者同时发生)这可能是您的程序没有打印任何内容的原因。 您可以尝试添加一些日志语句并检查顺序是否正确。

关于java - 字段超出范围java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31917665/

相关文章:

java - 线程生命周期如何在线程池中工作?

c# - 用于 TCP 监听的线程抛出 StackoverflowException

Java Selenium Web 元素变量与 Web 元素数组

java - 为什么 arrayList 中存储的每个对象的内容具有相同的值?

java - 从套接字的输入流中读取而无需在 JAVA 中忙等待

c# - C#中的多个并发定期刷新操作

java - Android JSON 接收问题

java - 在速度模板中同时迭代两个列表

java - 如何设置诺基亚应用程序开发环境?

java - IOException : Permission Denied