This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes How to Pause a Countdown Timer in a GUI Without Polling Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "How to Pause a Countdown Timer in a GUI Without Polling" Watch "How to Pause a Countdown Timer in a GUI Without Polling" New topic
Author

How to Pause a Countdown Timer in a GUI Without Polling

Eric Juneau
Greenhorn

Joined: Jul 13, 2009
Posts: 28
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.

Walter Gabrielsen Iii
Ranch Hand

Joined: Apr 09, 2011
Posts: 158
Okay, let's break your code down into smaller parts. Can you tell me why this won't work?



[spoiler]Hint: What does Thread's sleep() description say about monitors, aka locks?[/spoiler]
Walter Gabrielsen Iii
Ranch Hand

Joined: Apr 09, 2011
Posts: 158
Also, I think that your "isPaused" boolean is unsafe for multi-threading and that you should consider whether to mark it as volatile or use an java.util.concurrent.atomic.AtomicBoolean instead.
Walter Gabrielsen Iii
Ranch Hand

Joined: Apr 09, 2011
Posts: 158
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:
Eric Juneau
Greenhorn

Joined: Jul 13, 2009
Posts: 28
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: How to Pause a Countdown Timer in a GUI Without Polling
 
Similar Threads
How to stop a Timer after it's run more than once?
Popping in streaming audio
Create a counter that increments once a second until a given number of seconds is reached
Threads, and the UI question.
Java Threads: wait+notify