I am new to Java. My problem is that I am reading a file in a thread and creating a list of data records. This list of data records now needs to be accessed from another class.
Sample code -
public class ReadFile implements Runnable {
ArrayList<Integer> list = new ArrayList<Integer>(20);
public void run(){
// Code to read data from file and form the list.
}
}
Now this array list needs to be accessed from another class -
public class TestThread {
public static void main(String args[]) throws InterruptedException {
ReadFile readFile = new ReadBatchReReadFile();
Thread readBatchRecordThread = new Thread(readFile);
readBatchRecordThread.start();
List<Integer> list = readFile.getTenRecordList();
}
}
But the file is not read before I call the function to display the list. How can I make the thread to read the file and create the list to be executed first and then only the code to display the list to get executed. Else because the code to display the list is executed first I am getting an IndexOutOfBoundsException. Kindly advice on what I should do.
Thanks.