Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem with Timer and TimerTask

 
Karthik Veeramani
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Igor Ko
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Karthik Veeramani
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic