Two Laptop Bag*
The moose likes Java in General and the fly likes Problem with Timer and TimerTask Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Problem with Timer and TimerTask" Watch "Problem with Timer and TimerTask" New topic
Author

Problem with Timer and TimerTask

Karthik Veeramani
Ranch Hand

Joined: Dec 22, 2002
Posts: 132
I have some trouble using timers and timertasks... Heres the problem -
I have a class that implements TimerTask. A test code creates some Timer objects and calls schedule() with this TimerTask object. It goes something like this -
public void doIt() {
// _timer.cancel(); //NOTE : THIS LINE DOESNT SEEM TO IMPACT ANYTHING
//Re-start the timer.
_timer = null;
_timer = new Timer();
_timer.schedule(timertask, _timeout);
}
Now, if i call this doIt() method a couple of times, I get an IllegalStateException saying the timer is already cancelled (if i pass the same timertask object every time).
My understanding is that, if I create new Timer objects every time, and call schedule on the same timertask object, it shouldnt complain. Help me get rid of this exception.
Thanks
Karthik.


Thanks<br />Karthik<br />SCJP 1.4, CCNA.<br /> <br />"Success is relative. More the success, more the relatives."
Igor Ko
Ranch Hand

Joined: Jun 24, 2002
Posts: 90
Why you think that you can schedule the same TimerTisk in other
Timer ?
(probably TimerTask has some flag,that prevent it)
The normal way - or shedule with repeat, or create new TimerTask
to schedule again.
The other way (that I use ;-)) create your own version of
TimerTask and Timer, in your version you can enable rescheduling.
;-)
Karthik Veeramani
Ranch Hand

Joined: Dec 22, 2002
Posts: 132
ya possible... may b it has some flag to prevent it... but i really dont want to keep creating instances of that timertask object, i just want 1 instance to pass around... anyway thanks for the reply, i think i have to resort to ur way if nothing turns out right...
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
but i really dont want to keep creating instances of that timertask object, i just want 1 instance to pass around...
Well, I think you're stuck with how Timer and TimerTask are defined here. If you want to reschedule a task, the only way to do that is cancel the task and create a new one. This shouldn't be a big deal - a TimerTask is usually a fairly lightweight object. If you really want to re-use your task object for multiple schedulings for some reason, the best way I can think of is to define the reusable run() method in a separate Runnable, and create a TimerTask class which executes this Runnable's run() method:

Now you can re-use the underlying target object, as long as you create a new WrapperTask each time you reschedule:


"I'm not back." - Bill Harding, Twister
Karthik Veeramani
Ranch Hand

Joined: Dec 22, 2002
Posts: 132
This shouldn't be a big deal - a TimerTask is usually a fairly lightweight object. If you really want to re-use your task object for multiple schedulings for some reason

yes, that timertask object propagates to most part of my application, so my problem is not about the memory it consumes...
however, ur suggestion looks fine. I shall try that out. Thanks very much!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Problem with Timer and TimerTask
 
Similar Threads
Null pointer exception probably due to synchronisation issues; problem in starting activity
Timer & TimerTask - Name of Timer Object
Call action class from timertask's run method
Problem with java.util.Timer
What is the Linux command to find the created timer daemon