Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Tim Tock
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this
 
Mark Vedder
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can also take a look at example368. Scheduling a Timer Task to Run Repeatedly at www.javaalmanac.com
 
Tim Tock
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
cheers mark, the first example worked a treat!!
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, note that a simple Thread.sleep(long millis) might do the trick well enough at times.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic