Breaking out of an Infinite loop if user clicks a button
Posted on 1997-06-13
Here's my problem. I need to keep on looping through some calculations till the answer is found. While it's doing this I would like to have a pause button so that the user can change the variables in real time. The problem is that the loop will continue till the end. If I use the action() method to capture the mouse click (such as a pause button) the results will only be shown when the calculations are done.
Any idea of how to temporally stop the loop? I tried make the loop so that it looks like this:
while(running == true)
and set the running variable false when the user hit's pause but this is done when the answer is found.
I also thought of creating another thread for the buttons and pausing the default thread that to allow the other thread a chance. This brought up some questions also:
If I declare a thread such as (Thread my_thread null) is this the default thread that Java uses or is there another thread created every time a program runs? If this isn't the main thread does anyone know how to pause it and give control to another thread that may be waiting around?
Finally, someone suggested to me that I should use the
Thread.yield() in my loop to allow the action method a chance to do it's work but the loop continue's on till it's all done.