It's not a secret anymore!
The moose likes Threads and Synchronization and the fly likes Countdown timer in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Countdown timer in java" Watch "Countdown timer in java" New topic

Countdown timer in java

Peter Zagrzebski

Joined: Feb 08, 2009
Posts: 1
Hello I have two classes in java and am attempting to countdown to 0. I cannot change the code in minute second display and am having difficulty getting it to move from 10 to 0 on the display in minute second display because it calls invokeLater to Update the display and that does no get done until the end. If you could look at my code and give me any advice I would appreciate it.

Steve Luke

Joined: Jan 28, 2003
Posts: 4181

You are calling the countdown() method directly from the actionPerformed() method, which executes in your event dispatch thread - the same thread to used to draw on the screen. Since you are performing the Thread sleeping inside countdown the Event Thread won't be able to do the work it is intended to do - update the GUI.

So you should move the countdown() code to a different thread, so that the GUI interface isn't blocked while counting.

You are also doing some redundant context switching (with SwingUtilities.invokeLater() to call a method that itself uses SwingUtilities.invokeLater()), and creating objects you don't use (for example the t = new Thread(count)). So your code has a lot of clean-up which can be done.

I agree. Here's the link:
subject: Countdown timer in java
It's not a secret anymore!