• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem with Timer and TimerTask

 
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic