Okay I have a simulation running. I have implemented two JButtons which actionListeners on the GUI of my simulation. What I want is to have the whole simulation to pause if I press the pause button and to resume if I press the resume button.
There are multiple threads running, and I have tried to get each of the threads and invoke their wait() method when the pause button is clicked but I have not been successful in doing so.
Hence, I need some advice on how to do this. I’m using Swing for the GUI and the listeners work fine. I did try to invoke a sleep() and wait() on the current thread in the View class (using MVC pattern) just to see what happened but that caused the whole application to crash.
Any ideas?
You need to provide additional logic to your simulation thread that will accept “signals” from controlling (GUI) thread and wait for the next controlling signal to resume execution.
For example you can use
volatile boolean isPausedinstance field in simulation thread. Set it to true/false to pause/resume simulation thread. In simulation thread implement corresponding logic: