How to read data on the main thread from a file being read on a worker thread

419 Views Asked by At

I am processing a huge data file as "posted below", and I read it on a separate thread. And in the main thread i want to retrieve some data frm that file such as logFile.getFileHash.getTimeStamp to perform some operation on that timeStamp. The problem ia m facing is that, how to start reading some data from my file in the main thread only when my file is completely read in that other thread?

note: i do not want to do my operations on the data retrieved ffrom the file on the same thread that reads the file, i want to do so on the main thread.

example

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.
 }

file

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());
}
5

There are 5 best solutions below

0
On

Try using CountDownLatch to wait until the reader finishes: http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/CountDownLatch.html

1
On

Thread.join can fit this.

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

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

}
3
On

Add an attribute in the file thread and a public getter for it. When that thread finishes, change isFinished's value to true;

private boolean finished = false;
public isFinished(){...}

In your main thread, just sleep it and resume it to check if file thread has finished:

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
    ....
}
1
On

I'm not sure I understand the problem, but I think you are trying to make the main thread read the same file after the child thread has done reading it without ending the child thread. If that's so then you can create a synchronized readMyFile(File file) method that any thread can use to read any file, of course making sure the child thread reads the file first.

1
On

Sorry for the late reply.

If what I assumed was correct then you could do something like this, roughly...

public static void main(String args[]) {

    ...
    fileThread.start();

    synchronized (fileThread) {
        try{
            fileThread.wait();
        }catch(InterruptedException e){
            e.printStackTrace();
        }
    }
    ...
    MyReader.readMyFile(file);
    ...
}

... And the fileThread thread class something as ...

class FileThread extends Thread {

public void run() {

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

This is one way. I hope it helps.