先来看看源代码中关于 cancel的注释
/**
* Cancels the {@code Timer} and all scheduled tasks. If there is a
* currently running task it is not affected. No more tasks may be scheduled
* on this {@code Timer}. Subsequent calls do nothing.
*/
public void cancel() {
impl.cancel();
}
timer对象调用cancel后,再进行schedule任务调用会导致Crash
例如
Timer timer= new Timer();
timer.cancel();
timer.schedule(...) //crash
需要重新new timer对象
本文深入探讨了在Java编程中,如何正确理解和使用Timer对象的cancel和schedule方法,避免常见的错误导致的Crash问题,强调了重新实例化Timer对象的重要性。

5413

被折叠的 条评论
为什么被折叠?



