This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • 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:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

How to Pause a Countdown Timer in a GUI Without Polling  RSS feed

 
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!