I have two Java programs. The "first"/"Main" one implements a search algorithm and the "other"/"second" one uses Repast simulation framework for running a Multi-agent simulation. In the "first" Java program I have the following code block:
Class Individual{
public int getvalue()
{
While (Condition is true)
{
Start_MAS(); //start the simulation by calling the "second" Repast simulation program
//which will write some values to a text file at the end of the simulation
//read text file and do some work with the values
}
}}
the MA simulation/"second" program will run the simulation until a specific condition is met or 900 time-steps has elapsed, and once the stop condition is met, the program will write some output to a text file, which is read by the "first" program. Now my problem is that when I run the "first" program (which at some point calls/runs the second program/simulation), it does not wait for the simulation/"second" program to finish, but what happens is that after sometime of running the simulation/"second" program, the "first" program will continue running its own code which is reading the output text file so I get error that the text file is empty.
My question is how do I force the "first" program to wait or stop running its code until the "second"/ simulation program is finished (the stop condition has been met and output has been written to the text file)? I thought of using wait in the "first" program and notify in the "second" program as follows:
In the "first" program:
Class Individual{
public int getvalue()
{
While (Condition is true)
{
Start_MAS(); //start the simulation by calling the "second" program which will write some
//values to a text file at the end of the simulation
synchronized(this)
{
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//read text file and do some work with the values
}
}
In the "second" program:
public void step(){ //a function called in each time step in the simulation
If (Stop condition is met)
MyPackage.Individual.notify();
}
but if I do this, it will execute wait after returning from the "second" program, and also if I place it before the calling it will wait then start the simulation. Is using wait and notify the correct way to do this? Any suggestions or tips on how to accomplish this?
Thank you so much!