I am programming a simple countdown timer in a GUI. The timer has a start/pause toggle button that controls it. As you can see from the CounterThread class, in order to pause it, the method polls continuously until it gets unpaused. Is there a better way to do this? I've tried using the wait() and interrupt() methods in various ways, but they don't seem to work the way I want them to.
Pardon me if I use the older Threading methods, ie not the concurrent utilites, since I'm mostly a Java ME programmer. Here is an example of how synchronization and the wait() / notify() methods work:
First you should have a locking Object and a condition (boolean? int? whatever you want) to keep track of things (You might also add your own stuff too):
Here is how to set up run():
This would go in an update method (start/stop):
This would go in an "end the thread" method:
Joined: Jul 13, 2009
Sorry, but I'm not understanding how this applies to my problem. I don't have an end of thread method. My code isn't checking if it's "time to do something". The countdown timer should be paused. I tried to integrate what you wrote as best I could, but it doesn't work. When I do c.run(), it seems to lock all threads, and continuously loops, and it does not render the GUI fully.