Hey Rashi,

not sure I understand your question still.
You have X agent in each simulation, right? You have Y simulations you run.
Do you have Y threads running or do you have X*Y threads running? 

Anyway, "waiting for threads" is something you either do with Executors (see here for example) or more directly by  Semaphores
If all they need to do is to coordinate on killing a frame you might get lucky and just use an AtomicCounter that counts the time dispose() is called and when that number is equal to the number of agents that were supposed to call it then call the real dispose(), but that might not work for you.

Java Concurrency in practice is now 10 years old but it was a great introduction to both pratical concurrency and java 5 methods to deal with it and it's highly recommended.

On Tue, Oct 27, 2015 at 11:51 AM Rashi Aswani <[log in to unmask]> wrote:

I am sorry for not being clear earlier. I believe my question is a Mason related one. More specifically I want to run an experiment that will be repeated 100 times (each iteration has some randomly generated parameters). One obvious way is to run an iteration and then manually start the program. However this is time consuming, hence I am trying to automate the runs.  

In each iteration, there are multiple agents each modeled as thread in Mason. There is a display frame that gets created in each iteration which gets closed at the end of the iteration. In order to make it automated I am setting the display frame to null by using displayFrame.dispose() and then displayFrame = null after the run is completed so in the next iteration a new display frame comes up. However this is creating an issue since the old display frame was not getting killed. In order to make sure it gets killed correctly, I had to put in an arbitrary sleep so that all the threads are finished and then the frame is killed. However the sleep is leading to:
(a) Long run times (hours) once I set up more runs.
(b) As I am making the frame null, it is not freeing up the memory hence I get memory dump after some point. I also tried using unregisterFrame() and unregisterAllFrames() but none of these worked.

In short, I'm trying to find out,
(a)Is there a functionality in Mason that can detect when all the threads are done so we know when to close the display frame?
(b)Is there a way to close the display frame instead of specifically nullifying it?


On Mon, Oct 12, 2015 at 11:09 PM, Eric 'Siggy' Scott <[log in to unmask]> wrote:


How to wait for threads until they complete is a general Java question, not a Mason question.  General multithreaded programming is out of the scope of this list.

Of course, perhaps I've misunderstood your question, and you are using something Mason-specific to control your threads.  In that case perhaps you could give us a little more detail about what you're doing and what you'd like to do.



On Mon, Oct 12, 2015 at 11:18 AM, Rashi Aswani <[log in to unmask]> wrote:

I'm using mason for a simulation having various threads and I'm trying to run the same simulation again with different parameter values. I'm currently using thread.sleep (to pause main thread) before creating one more simulation (in next iteration) so as provide enough time for existing simulation to run.
It would be great to know if there is any other way of handling threads, as the sleep increases runtime to a great extent.
Thanking you in advance.

Rashi Aswani


Ph.D student in Computer Science, George Mason University
CFO and Web Director, Journal of Mason Graduate Research