0

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.

bably
  • 1,065
  • 5
  • 17
  • 27
  • 1
    You seem to be looking for [this](http://stackoverflow.com/questions/4691533/java-wait-for-thread-to-finish) – Robin Topper Apr 21 '17 at 07:17
  • @RobinTopper Thanks. thread.join() did the the trick as mentioned in the post you shared. If you put it in an answer I will accept it. – bably Apr 21 '17 at 07:25
  • You're welcome. Won't make an answer out of it, since this question should probably be marked as a duplicate of the question I linked – Robin Topper Apr 21 '17 at 07:26
  • Use FutureTask (http://www.journaldev.com/1650/java-futuretask-example-program) for reading from file. – MicD Apr 21 '17 at 07:29
  • Possible duplicate of [Java Wait for thread to finish](http://stackoverflow.com/questions/4691533/java-wait-for-thread-to-finish) – Robin Topper Apr 21 '17 at 07:33
  • If you just join, then you don't need a thread and may as well run the reading method on the main thread. – assylias Apr 21 '17 at 07:37

1 Answers1

0

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.

bowmore
  • 10,842
  • 1
  • 35
  • 43
Bhargav Modi
  • 2,605
  • 3
  • 29
  • 49