Using Java Timer to build a countdown timer

Whilst developing an application the other day I wanted to create a repeating task.  Looking around for a solution I came across the Timer and TimerTask classes.  These two classes in combination looked like a perfect solution to my problem.  It was only when I came to implement my solution I noticed something about how they work.  It’s obvious when you think about it, but at the time it caught me out.  I thought I would share my experience with you in case you are looking for a means of creating a repeating task and choose to use these classes.

The 9 minute video below walks through a simple countdown program using objects based on the Timer and TimerTask classes.  You can see the issue as we step through the debugger and then look at how in this instance the issue was handled.

The java.util.Timer and java.util.TimerTask have been available since Java version 1.3.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.