Q:

Java - How to form an arraylist in a thread first before it is displayed

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. 

A:

Few points to take a note 
Use join() method of Thread. 
public class TestThread {
public static void main(String args[]) throws InterruptedException {
  ReadFile readFile = new ReadBatchReReadFile();
  Thread readBatchRecordThread = new Thread(readFile);
  readBatchRecordThread.start();
  readBatchRecordThread.join(); //here join method will work for you. 
  List<Integer> list = readFile.getTenRecordList();
}

Or You can use concurrency API's CountDownLatch class
CountDownLatch latch = new CountDownLatch(1); //here 1 indicates that only one thread is present

public class TestThread {
  public static void main(String args[]) throws InterruptedException {
  ReadFile readFile = new ReadBatchReReadFile();
  Thread readBatchRecordThread = new Thread(readFile);
  readBatchRecordThread.start();
  latch.await(); //here await method will work for you. 
  List<Integer> list = readFile.getTenRecordList();
}

In Run method of ReadFile make one change in the last line of it.  use below code
latch.countDown();

needless to say that you need to create CountDownLatch object properly as it should be accessible in both classes.