• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Executing a fn every hour

 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want to run a java program that calls a function defined in it's class every hour. I've never done such a thing (only META refresh in HTML, hahaha).
Could anyone point me how to do this? I would run the java program from the command line and the logic to do the calling would reside in the main().
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's a suggestion:

I would recommend starting a separate thread rather than keeping everything in main().
 
David Duran
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Marilyn!
Clarification: Is it necessary to call the function before the try {...} block? Is the difference simply:
function() ... 1 hour ... function() versus
...1 hour... function()?
 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check out the java.util.Timer and the corresponding java.util.TimerTask classes. They will do exactly what you want.
HTH,
Matt
 
Marilyn de Queiroz
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by David Duran:
Clarification: Is it necessary to call the function before the try {...} block? Is the difference simply:
function() ... 1 hour ... function() versus
...1 hour... function()?


Yes. That's the only difference.
 
David Duran
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Matt, the Timer class looks like it would do the job but this code is going into an RMI server app which already extends UnicastRemoteObject. Since Timer requires a TimerTask object as one of the parameters to it's schedule functions that would mean I'd have to extend TimerTask (since it's abstract) within this class.
Since I can't extend 2 classes I guess I can't use TimerTask and Timer (unless I'm mistaken). Right now I'm able to implement threads because I'm implementing Runnable as opposed to extending Thread.
 
Ranch Hand
Posts: 388
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
try javax.swing.Timer
it takes an actionListener so no need to extend, jsut implement the interface.
k
 
reply
    Bookmark Topic Watch Topic
  • New Topic