Java 如何从工作线程上正在读取的文件中读取主线程上的数据

Java 如何从工作线程上正在读取的文件中读取主线程上的数据,java,multithreading,concurrent-programming,Java,Multithreading,Concurrent Programming,我正在处理一个巨大的数据文件“张贴在下面”,我在一个单独的线程上阅读它。在主线程中,我想从该文件中检索一些数据,比如logFile.getFileHash.getTimeStamp,对该时间戳执行一些操作。我面临的问题是,只有当我的文件在另一个线程中被完全读取时,如何在主线程中开始从我的文件中读取一些数据 注意:我不想在读取文件的同一线程上对从文件中检索到的数据执行操作,我想在主线程上执行操作 示例 public static void main(String[] args) throws IO

我正在处理一个巨大的数据文件“张贴在下面”,我在一个单独的线程上阅读它。在主线程中,我想从该文件中检索一些数据,比如
logFile.getFileHash.getTimeStamp
,对该时间戳执行一些操作。我面临的问题是,只有当我的文件在另一个线程中被完全读取时,如何在主线程中开始从我的文件中读取一些数据

注意:我不想在读取文件的同一线程上对从文件中检索到的数据执行操作,我想在主线程上执行操作

示例

public static void main(String[] args) throws IOException {

//fileThread.start(); will take 8 seconds.
/*
 *here i want to read some data from my file to perform some processing but only
 *when the thread that handles reading the file finishes work.
 }
private static void processFile(File dataFile) throws IOException {
    // TODO Auto-generated method stub
    MeasurementFile logfile = new MeasurementFile(dataFile);
    System.out.println(logfile.getTotalLines());
  System.out.println(logfile.getFileHash().get(logfile.getTotalLines()).getFullParameters().length);
    System.out.println(logfile.getFileHash().get(logfile.getTotalLines()).getEngineSpeed());
}
文件

public static void main(String[] args) throws IOException {

//fileThread.start(); will take 8 seconds.
/*
 *here i want to read some data from my file to perform some processing but only
 *when the thread that handles reading the file finishes work.
 }
private static void processFile(File dataFile) throws IOException {
    // TODO Auto-generated method stub
    MeasurementFile logfile = new MeasurementFile(dataFile);
    System.out.println(logfile.getTotalLines());
  System.out.println(logfile.getFileHash().get(logfile.getTotalLines()).getFullParameters().length);
    System.out.println(logfile.getFileHash().get(logfile.getTotalLines()).getEngineSpeed());
}

尝试使用CountDownLatch等待读卡器完成:

可以满足此要求

public static void main(String[] args) throws IOException {

fileThread.start(); 
fileThread.join();
//..now this thread blocks until *fileThread* exits

}

在文件线程中添加一个属性和一个公共getter。当该线程完成时,将isFinished的值更改为true

private boolean finished = false;
public isFinished(){...}
在主线程中,只需休眠它并恢复它,以检查文件线程是否已完成:

public static void main(String[] args) throws IOException {
    ...
    fileThread.start();
    ...
    while(!fileThread.isFinished()){
        try{
            Thread.sleep(1000);//1 second or whatever you want
        }catch(Exception e){}
    }
    //Do something
    ....
}

我不确定我是否理解这个问题,但我认为您正在尝试使主线程在子线程完成读取后读取相同的文件,而不结束子线程。如果是这样的话,那么您可以创建一个同步的readMyFile(File File)方法,任何线程都可以使用它来读取任何文件,当然要确保子线程先读取文件。

很抱歉回复太晚

如果我的假设是正确的,那么你可以这样做,大致上

public static void main(String args[]) {

    ...
    fileThread.start();

    synchronized (fileThread) {
        try{
            fileThread.wait();
        }catch(InterruptedException e){
            e.printStackTrace();
        }
    }
    ...
    MyReader.readMyFile(file);
    ...
}
。。。fileThread线程类是

class FileThread extends Thread {

public void run() {

    synchronized (this){
        ...
        MyReader.readMyFile(file);
        notify();
        ...
    }
}

这是一种方式。我希望它能有所帮助。

完成后
至少应该是易变的。这并没有错。Volatile用于指示变量的值将由不同的线程修改,而不是由主线程读取。这个解决方案是完全线程安全的,事实上,你只是用一个只被其中一个修改过的变量来与线程进行通信。我们在这里看到的是读取变量的主线程和修改变量的文件线程。如果有两个线程访问一个变量(即使只有一个线程写入),那么该变量必须是可变的。这是java内存模型所必需的。否则(没有volatile),jvm可以自由缓存非volatile变量的值,并且从不让主线程观察其他线程写入它的任何值。这只在工作线程读取文件后结束时起作用。如果它应该做一些其他的工作,那么它不是最好的匹配。谢谢你的回答,我想我需要做一些类似同步的事情。根据上面的代码,你能告诉我如何使用同步吗?谢谢你的回答。您能否解释一下它是如何工作的,以及主线程和文件线程是如何同步的?