TimerTask执行后会发生什么?
执行 run 方法后,扩展 TimerTask 的类会发生什么情况? myTask 的值是 null 还是执行后会发生什么?
MyTask myTask = new MyTask();
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();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您从一个已经结束的方法启动它(并且没有在任何地方引用它,例如在仍然存在的对象的成员变量中),它将被垃圾收集器清理。
除非任务保留对大量内存的引用,否则无需将其设置为 null。
如果您确实需要取消引用该任务,您应该在其 run() 方法末尾添加一个调用,以从您引用它的任何地方丢弃它。
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.
没有什么。您可以查看
Timer
类,以了解调度
TimerTask
时幕后实际发生的情况。Nothing. You can check the source code of the
Timer
class, to understand what is really happening under the hood when aTimerTask
is scheduled.