• Post Reply Bookmark Topic Watch Topic
  • New Topic

Timer.schedule....

 
aman hindustani
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello to all.
i am writing a Thread which has to start at 23 hrs ...and for every 24 hours it has generate a report..and so..
my doubt is which method of timer has to be used.

timer.scheduleAtFixedRate(task,starttime,delaytime)
or
timer.schedule(new ReportGenerator(), cal.getTime(),(24*60*60*1000));

which one is best to use both in terms of accuracy and in terms of using resources(should use least resources..)





Timer timer = new Timer();
Calendar cal = Calendar.getInstance();

// setting the time ....so that programme can start here it is 23:00 hrs
cal.set(Calendar.HOUR_OF_DAY,23);
cal.set(Calendar.MINUTE,0);
cal.set(Calendar.SECOND,0);
cal.set(Calendar.MILLISECOND,0);

// allow to run back again after 24 hrs
timer.scheduleAtFixedRate(new ReportGenerator(), cal.getTime(), (24*6060*1000)); or

timer.schedule(new ReportGenerator(), cal.getTime(), (24*6060*1000));








Thanks & Regards,
Aman...
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the doc, it looks like FixedRate is what you need.

Fixed-rate execution is appropriate for recurring activities that are sensitive to absolute time, such as ringing a chime every hour on the hour, or running scheduled maintenance every day at a particular time.

The other schedules are "fixed delay" so if the tasks take a few minutes to run they'll delay 24 hrs from finish to next start and get later every day.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!