jQuery in Action, 2nd edition*
The moose likes Beginning Java and the fly likes performing some code, every 5 seconds using system clock possibly?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "performing some code, every 5 seconds using system clock possibly??" Watch "performing some code, every 5 seconds using system clock possibly??" New topic
Author

performing some code, every 5 seconds using system clock possibly??

Tim Tock
Greenhorn

Joined: Jan 15, 2004
Posts: 15
Hi,
Is it possible to have a java program perform some operation every x amount of seconds, something like{
(every 5 seconds) do{
system.out.println("I print out every 5 seconds");
}
obviously the code inbetween the brackets is a little bit more complicated than that
Thanks
Timmy
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
Try this
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

You can also take a look at example368. Scheduling a Timer Task to Run Repeatedly at www.javaalmanac.com
Tim Tock
Greenhorn

Joined: Jan 15, 2004
Posts: 15
cheers mark, the first example worked a treat!!
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Also, note that a simple Thread.sleep(long millis) might do the trick well enough at times.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Dirk Schreckmann:
Also, note that a simple Thread.sleep(long millis) might do the trick well enough at times.

Note that this doesn't guarantee an exact amount of time that is paused. If the OS is busy doing something else when the time runs out, it may well keep doing what it is doing rather than restarting your program.
I guess I shouldn't go too deep into this issue since it really isn't a beginner topic. I just thought I'd mention it.
Layne


Java API Documentation
The Java Tutorial
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
I think that's a good thing to point out.
Just because we're beginners, doesn't mean it hurts us to learn some detail that might be good to know about a little bit down the line. So, please don't hesitate to mention the occasional detail, especially if it's important and might help to create an understanding of an issue.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: performing some code, every 5 seconds using system clock possibly??