Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes stopping Timer after certain number of actions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "stopping Timer after certain number of actions" Watch "stopping Timer after certain number of actions" New topic
Author

stopping Timer after certain number of actions

John Greenauer
Greenhorn

Joined: Jun 29, 2010
Posts: 4
Hi,

This is my first post here, so please bear with me.

I want to make my simple program print "Action" 5 times and then stop. The code below will print "Action" once per second forever. If I un-comment the timer.cancel(); it stops before it even prints once. I've tried putting the timer inside a for loop and inside a while loop, but can't make them work.

What am I missing?

Thanks in advance.

- Johnny

Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Hi, welcome to the ranch
I enclosed your source in [code] tags. Please UseCodeTags the next time you post some code.

About your problem, why don't you put the counter variable inside the anonymous TimerTask, increment it and cancel when it reaches five ?


[My Blog]
All roads lead to JavaRanch
John Greenauer
Greenhorn

Joined: Jun 29, 2010
Posts: 4
Thanks for your reply

I'm not having much success with that. The code below causes "Action to be print 5 times immediately, every second (after the 1 second delay). I believe that the 1 second delay in the timer method does not prevent the computer from running through the for loop quickly. So, 5 timers are created in less than a second and they all activate after the one second delay. Also, if the timer.cancel(); is un-commented, the program prints nothing because it ends before the one second delay has run it's course. I've tried a slightly different version of this with the timer.cancel(); placed within an if( count == 4) loop. It also prints nothing.

Perhaps the timer method will not work the way I want it to. Maybe, I need a different method. Any thoughts?



Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

I'm not talking about looping 5 times in the TimerTask. The Timer will call the task, that's its job, not yours. What I'm talking about is to put the "int counter" inside the TimerTask, to increment it when the task is called (i.e in the run() method), and to cancel the timer when the counter reaches 5.
John Greenauer
Greenhorn

Joined: Jun 29, 2010
Posts: 4
O.K. I finally got it. That definitely took longer than it should have.

I was getting an error message with timer.cancel(); , but System.exit(0); worked straight off. I've put the final product below for any rookies (like me) that might look for it.

Thanks for the help.

Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

I was getting an error message with timer.cancel(); , but System.exit(0); worked straight off.

Dooh this.cancel() would do the trick.
John Greenauer
Greenhorn

Joined: Jun 29, 2010
Posts: 4
Ahh. Yes, that does work. I haven't gotten to "this" in my book yet. I see that it stops printing "Action" without stopping the program. I can see how that's probably a better option in most cases. Thanks a lot.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: stopping Timer after certain number of actions
 
Similar Threads
errorr in compling.. help
Timer thread gets Exception when the Syste time is changed
Help me please ... Timer
Scheduled check ...
TimerTask and Schedule