• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

How to Pause a Countdown Timer in a GUI Without Polling

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
    Bookmark Topic Watch Topic
  • New Topic