0

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!

Evan
  • 207
  • 2
  • 3
  • 12
  • without showing actual details, no one will be able to help you. – jtahlborn Jul 12 '14 at 00:24
  • 1
    You usage of the terms "project" is ambiguous. Do you mean a program or application? We also do not know what "Repast" means. Your requirements are too vague and confusing. Can you post some example code than is runnable? You may want to research "Java concurrency". [How to create a Minimal, Complete, Valid Example.](http://stackoverflow.com/help/mcve) – javajon Jul 12 '14 at 00:27
  • I'm assuming repast is the simulation framework that shows up in a search. Regardless, I'll have to agree that we probably need more details. Odds are high you are best off using a Callable/Future to simplify your code such that you don't have to do the wait/notify. – Josh Berry Jul 12 '14 at 00:36
  • 1
    You could have them both open the same socket - the waiting program won't be able to open the socket until the first program releases it. – Bohemian Jul 12 '14 at 00:44
  • Projects only exist in an IDE. Do you mean 'program'? – user207421 Jul 12 '14 at 00:50
  • Sorry if I was not clear. I added some more details. @javajon what is the difference between a program and application? what I meant by project is an organizational unit that includes packages that contains multiple classes. – Evan Jul 12 '14 at 00:55
  • @ejp Yes I use Eclipse IDE so It is a project that contains multiple classes and the code above is in one class. – Evan Jul 12 '14 at 01:00
  • 1
    Projects don't run. Programs run. Please clarify your question. – user207421 Jul 12 '14 at 02:54
  • program.equals(application). Project is Eclipse lingo, not to be confused with an actual java process running with a [pid](http://en.wikipedia.org/wiki/Process_identifier). – javajon Jul 12 '14 at 03:03
  • Do they need to be separate processes? You could use a separate thread and call thread.join(). Else just call the process... process.waitFor() works too. http://docs.oracle.com/javase/tutorial/essential/concurrency/procthread.html – u8sand Jul 12 '14 at 04:26
  • @u8sand actually they are not separate processes. Is using a separate thread recommended as the loop will be executed hundreds to thousands number of times? is not expensive to create a new thread each iteration? – Evan Jul 12 '14 at 22:17
  • Please help what is the best and most efficient way to achieve this? – Evan Jul 12 '14 at 22:19

1 Answers1

0

I suspect your Repast simulation is genuinely terminating early due to some difference in conditions between running it programatically from your first app and when you have tested the second program (Repast simulation) stand alone.

Therefore - I recommend that you put some debug statements in your stop condition block to check whether the simulation is genuinely stopping before returning. If so, you have a different question to ask about why the simulation is stopping, rather than one on processes/threads. e.g.

...
If (Stop condition is met)
{
    System.out.println("Stop condition has been met at tick " + 
      RepastEssentials.getTickCount());
}
...

I also suggest you join the repast-interest list to get answers to these questions (I am not a member of the Repast project, but do use it regularly). These kinds of questions are often asked and resolved, the list is active.

N.B. most recent thread on this exact issue was resolved here (posted for reference - although looking at the numbers in there, maybe that question is from you?).

J Richard Snape
  • 20,116
  • 5
  • 51
  • 79