my dog learned polymorphism*
The moose likes Beginning Java and the fly likes Question about Timer class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Question about Timer class" Watch "Question about Timer class" New topic
Author

Question about Timer class

Ronald Castillo
Ranch Hand

Joined: Apr 16, 2011
Posts: 47

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
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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

Joined: Apr 16, 2011
Posts: 47

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
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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

Joined: Apr 16, 2011
Posts: 47

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. ;)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question about Timer class