• Post Reply Bookmark Topic Watch Topic
  • New Topic

Using Timer Class  RSS feed

 
john johns
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I'm trying to build a very simple countdown timer. When I toggle a breakpoint to my listener for debug, it seems like it's not reacting.

Here's my code. There's no stopper yet. I just want it to respond correctly.





I added the Counter to listen to listen to the timer. Shouldn't it respond from the tTimer.start(); ? It seems I'm missing something very important.
 
Paweł Baczyński
Bartender
Posts: 2086
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

Javadoc of javax.swing.Timer says:
Although all Timers perform their waiting using a single, shared thread (created by the first Timer object that executes) (...)

So, Timer works on a separate thread. It looks like it is a deamon thread because the application quits after main thread quits.
Put Thread.sleep(1000000L) after tTimer.start() and see what happens.

BTW, javax.swing.Timer is best suited for work with GUI. Why do you use it in a console application?
Why not use java.util.Timer?
Or even better java.util.concurrent.ScheduledThreadPoolExecutor?
 
john johns
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the kind welcome and I appreciate your help!

I added Thread.sleep and it does work. But I'm still not sure what it does, or even what the term Thread means. I think I have to spend some more time staring at this.

I just completed my first programming class during the summer. We worked with GUI a bit in the end, and the teacher mentioned that you can add a timer to the GUI program, but we never really dove into it. So I've been looking up various tutorial to try to implement it.

I've seen javax.swing.Timer and java.util.Timer in the suggestion while I was coding, but wasn't sure what the difference was. The program I'm trying to make does work with GUI. I want the countdown timer that starts at 15, and when they click the button, it resets back to 15 and repeats.

This was the tutorial I was trying to emulate in a simpler form to just make the console yell out the number:
https://www.youtube.com/watch?v=gs5aMzlLLts

I was able to get most of what he was doing, but the timer class still confuses me. It looks like I still have a lot more reading to do.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
john johns wrote:I added Thread.sleep and it does work. But I'm still not sure what it does

Calling sleep() putts the currently executing thread to sleep for the specified time.
The problem you are seeing as Pawel stated is the main thread is exiting the main method before the Timer has executed it's task and as the timer is run on a daemon thread the application immediately closes down. Putting the sleep() in there stops the main thread from exiting the main method for the specified time and hence gives time for the Timer to run.

or even what the term Thread means. I think I have to spend some more time staring at this.

I suggest you read an article on threads such as this one
 
john johns
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:
john johns wrote:I added Thread.sleep and it does work. But I'm still not sure what it does

Calling sleep() putts the currently executing thread to sleep for the specified time.
The problem you are seeing as Pawel stated is the main thread is exiting the main method before the Timer has executed it's task and as the timer is run on a daemon thread the application immediately closes down. Putting the sleep() in there stops the main thread from exiting the main method for the specified time and hence gives time for the Timer to run.


Thanks for the further explanation and I'll definitely take a look at that link.

I think understand what you both mean now. I implemented it with the GUI program and it did exactly what what I wanted because the thread remains active based on what you guys said if I'm understanding correctly.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!