java.util.Timer
I'm getting unexpected results from theschedule method of the Timer class. A shortened version of my code is below.
My understanding is that theschedule method is supposed to trigger a reoccurring event where theperiod (sleepTime in my code) is supposed to be the time from theend of one event to thebeginning of the next. What I'm getting is behavior that is consistent with what I understand the methodscheduleAtFixedRate is supposed to exhibit, which is thatperiod appears to be the time from thebeginning of one event to thebeginning of the next. I also tried thescheduleAtFixedRate, but the results are the same, except that is what is expected fromscheduleAtFixedRate.
Just FYI - the runRule method takes anywhere from 20-60 seconds to return (as opposed to a fraction of a second), so it's easy to see where the timing is starting.
Any help I could get would be greatly appreciated.
God Bless,
Chris
long sleepTime= 60;
long initialSleep = 30;
Timer timer =new Timer();
RunRuleTask rrt =new RunRuleTask(env, ruleName, dcom);
timer.schedule(rrt, initialSleep, sleepTime);
publicclass RunRuleTaskextends TimerTask
{
publicvoid run()
{
try{
dcom.runRule(ruleName);
}catch (Exception ignore){}
}
}
[1991 byte] By [
G35a] at [2007-9-29 17:12:33]

> My understanding is that the schedule method is
> supposed to trigger a reoccurring event where the
> period (sleepTime in my code) is supposed to be
> the time from the end of one event to the
> beginning of the next.
That's not correct.
From schedule(): "In fixed-delay execution, each execution is scheduled relative to the actual execution time of the previous execution."
From scheduleAtFixedRate(): "In fixed-rate execution, each execution is scheduled relative to the scheduled execution time of the initial execution."
You could schedule one time executions and have your taks reschedule itself at the end of excution if it weren't for an annoying at AFAIK pointless feature of TimerTasks that makes it impossible to schedule the same instance more than once or reschedule it after it has been cancelled.
So if you want to do something like that, you'd have to create a new TimerTask at the end of each execution.
Thanks for all of the great replies.
I originally got my understanding of the workings of the Timer class from the 3rd edition of "java in a nutshell", which has Timer in it, but as a beta in version 1.3. It explicitly talks about the timing starting from either the end or beginning of the previous execution.
Anyway, I understand the difference now. I will either have to do manual scheduling of each task, or just not use Timer at all, which is what I was doing before I realized Timer existed.
G35a at 2007-7-15 15:55:36 >
