Link to home
Start Free TrialLog in
Avatar of srinitin
srinitin

asked on

How to change TimerTask period at runtime without using new TimerTask()

Hi,

I want following functionality in my TimerTask (MyTimerTask) class:

1. First execute at startTime and then on variable interval say start_time = 11 pm and then interval {1,3,10}
so it executes at 11 pm, 11.01, 11.03, 11.10 and then every hour with same interval.
i.e 12 pm, 12.01, 12.03, 12.10 ..until it is not cancelled.

2. start_time and interval both should be configurable at runtime  without using new instance of timertask object.    i want to maintain my timer task object.

3. example to change scheduling at runtime:
          a. mytimerTask.changeStartTime(new time)
          b. mytimerTask.changeInterval(new interval)
          c. mytimerTask.changeStarTimeAndInterval(newTime, new interval)

I have already achieved this functionality in my class but to achieve this i have used new myTimerTask() each time when rescheduled.
and lost my original object. so configuration on earlier objectis not possible as the running timertask object is different (new TimerTask()).

How to achieve this without loosing timertask object. ( Is it possible to reschedule it using this operator?)

TIA
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

>>How to achieve this without loosing timertask object.

Why is that a problem?
Avatar of srinitin
srinitin

ASKER

My 3rd point explain it. if i loose my original object as running timer how this affects:


3. example to change scheduling at runtime:
          a. mytimerTask.changeStartTime(new time)
          b. mytimerTask.changeInterval(new interval)
          c. mytimerTask.changeStarTimeAndInterval(newTime, new interval)

as the running timertask object is different (new TimerTask()) with original TimerTask object (mytimerTask)
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Probably u are right. How do i know when timertask get executed. I mean is there any way to get callback?
No you can't (do it via the TimerTask). The TimerTask knows nothing about when it is scheduled to be run. That is handled by the Timer.
What you would need to do is keep a reference to the TimerTask and cancel() it with the Timer, and reschedule as required.
>>I mean is there any way to get callback?

You can call a method, yes. But why would you not know when it gets executed?
Thanks CEHJ.

:-)
Creating a new TimerTask won't work, because the existing one will continue to execute (and its a bit error prone).
You need to instead do what I suggested above.
>>because the existing one will continue to execute

No it won't - unless for some odd reason you fail to cancel the Timer
Forgot to mention, the other reason not to create new timertask instances, you should avoid object proliferation at all cost. Number one cause of Java applications performing badly.
So thats 3 good reasons not to do it, not to mention that you don't need to.
Let me know if you have any questions :)