views:

101

answers:

2

What happens to class that extends TimerTask after it's run method has been executed? Is value for myTask null or what after execution?

MyTask myTask = new MyTask();
A: 

Nothing. You can check the source code of the Timer class, to understand what is really happening under the hood when a TimerTask is scheduled.

JG
How can I set it to null after execution ?
newbie
Don't worry about that, the GC will eventually free the resources occupied by it, when it's no longer needed.
JG
+1  A: 

If you started it from a method that has since ended (and didn't reference it anywhere, e.g. in a member variable of an object that's still alive) it will be cleaned up by the garbage collector.

There's no need to set it to null unless the Task keeps references to huge amounts of memory.

If you really need to de-reference the Task you should add a call at the end of its run() method to discard it from wherever you are referencing it from.

Stroboskop
Ok, I need to run this task in intervals, but I found out that I can set task to run automatically in intervals using public void schedule(TimerTask task, long delay, long period)
newbie
I thought you were using `schedule` the whole time, that's why I suggested you to read the `Timer` source.
JG