?
Solved

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

Posted on 2007-08-09
11
Medium Priority
?
1,654 Views
Last Modified: 2013-11-05
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
0
Comment
Question by:srinitin
  • 5
  • 3
  • 3
11 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 19665165
>>How to achieve this without loosing timertask object.

Why is that a problem?
0
 

Author Comment

by:srinitin
ID: 19665428
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)
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 1000 total points
ID: 19665697
I don't really see a problem. The attributes that you want to change are applied to the Timer, not the TimerTask. All you need is a class that aggregates a Timer and TimerTask. When its 'when' and 'interval' (for example) attributes are changed, this simply changes the Timer and a new TimerTask is created
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:srinitin
ID: 19666080
Probably u are right. How do i know when timertask get executed. I mean is there any way to get callback?
0
 
LVL 92

Expert Comment

by:objects
ID: 19666545
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.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 19666930
>>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?
0
 

Author Comment

by:srinitin
ID: 19671748
Thanks CEHJ.

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 19671973
:-)
0
 
LVL 92

Expert Comment

by:objects
ID: 19673644
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.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 19673667
>>because the existing one will continue to execute

No it won't - unless for some odd reason you fail to cancel the Timer
0
 
LVL 92

Expert Comment

by:objects
ID: 19673754
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 :)
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
Make the most of your online learning experience.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Suggested Courses
Course of the Month15 days, 8 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question