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

Question about Timer class

 
Ronald Castillo
Ranch Hand
Posts: 47
Java MySQL Database PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello ranch,

I'm going to create a Java app to run as a Windows Service (using the Tanuki Software Wrapper). My app is supposed to run EVERYDAY at a fixed time, let's say: 03:00AM.

My question is, when using the Timer schedule method, if I don't specify the PERIOD, will the task run everyday or I must set a period (in my case equal to a whole day in milliseconds)?

Also, is Timer the best way to run a task at a fixed time? I mean, my application will ALWAYS be running on the background so it can perform the operation at 3:00AM. I'm really concerned about performance and memory usage.
I would've used a scheduled task instead, but they don't want me to (-_-" don't ask)...

Thanks for the help in advance
 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, if you use a Timer to cause your application to do something every 24 hours, then it's going to be permanently in memory. But you're using Windows, so it's going to be swapped out to disk when it isn't doing anything anyway. So I wouldn't make much of a fuss about memory usage.

Incidentally if you want something to happen at 3 AM every day, then "every 24 hours" isn't an accurate way to do that if you live in a place which observes daylight saving time.
 
Ronald Castillo
Ranch Hand
Posts: 47
Java MySQL Database PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Incidentally if you want something to happen at 3 AM every day, then "every 24 hours" isn't an accurate way to do that if you live in a place which observes daylight saving time.

Not worried about that, when I'm from we don't use daylight saving time.

Yes, if you use a Timer to cause your application to do something every 24 hours, then it's going to be permanently in memory.


So, we could say this would be enough (example):


Or do I need to specify the 24 hours (in milliseconds) on the following line:
timer.schedule(new AlarmTask(), alarmTime, hoursInMilliseconds);
 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good question. So what does the API documentation say about those methods?

For your first choice it says
Schedules the specified task for execution at the specified time. If the time is in the past, the task is scheduled for immediate execution.


So if the time you provide it is 2011 September 10 03:00:00 then it runs then. I don't see anything which says it will run again at some future time, so I wouldn't assume it would. However you could try it and see.

I will leave you to read the docs for the other method you mentioned and see if that method does what you want.
 
Ronald Castillo
Ranch Hand
Posts: 47
Java MySQL Database PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:Good question. So what does the API documentation say about those methods?

For your first choice it says
Schedules the specified task for execution at the specified time. If the time is in the past, the task is scheduled for immediate execution.


So if the time you provide it is 2011 September 10 03:00:00 then it runs then. I don't see anything which says it will run again at some future time, so I wouldn't assume it would. However you could try it and see.

I will leave you to read the docs for the other method you mentioned and see if that method does what you want.


I did read the docs, however, wanted to know if someone had tried it. Didn't want to wait a whole day to find out. But, I'm guessing it wont run again, so I'm gonna go with schedule(TimerTask task, Date firstTime, long period) or scheduleAtFixedRate(TimerTask task, Date firstTime, long period) (I'm aware of the difference between them).

Thanks for the help Paul. I love the ranch, I can always get huge help from great people. ;)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic