Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Canceling a TimerTask

 
Jean-Michel Vilain
Greenhorn
Posts: 27
Chrome IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ranch people!
I'm into server side multithreading and I'm finding it puzzlingly amusing.
I create a Timer and use it to schedule a TimerTask via schedule(TimerTask t, int delay), i.e. this TimerTask is meant to trigger once at most.
In the case where the TimerTask gets canceled, is this safe not keeping a reference to the Timer? Because I won't be able to call stop or anything on it when the TimerTask gets canceled.
Thoughts or suggestions welcome!
 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The procedure to cancel a TimerTask seems pretty simple to me: you call its cancel() method. You can read the API documentation and confirm that for yourself. So I don't understand the rest of your question.
 
Chris Hurst
Ranch Hand
Posts: 443
3
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's safe as the timer lives on until actually removed by the scheduler (happens automatically) when cancelled so if your not worried about performance your fine . Cancelled timers can seriously mess with GC performance (consider purge), though only worry about this if your writing serious server apps and use timers a lot.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic