我在我的网络应用程序和 onContextDestroyed 处理程序中使用计时器任务,我使用以下代码使所有内容为空:
System.out.println("contextDestroyed - fileWatcherTask:"+fileWatcherTask);
System.out.println("contextDestroyed - mytimer:"+mytimer);
fileWatcherTask.cancel();
mytimer.cancel();
System.gc();
if (fileWatcherTask != null) {
System.out.println("fileWatcherTask is not null");
fileWatcherTask=null;
}
if (mytimer!=null)
{
System.out.println("mytimer is not null");
mytimer=null;
}
System.out.println("contextDestroyed - logTimerTask After invoking cancel mytimer: "+mytimer);
System.out.println("contextDestroyed - logTimerTask After invoking cancel fileWatcherTask: "+fileWatcherTask);
但是,有时它会打印为 null,有时则不是。谁能告诉我问题是什么?如果它不为空,我会得到以下 Catalina 日志输出:
严重:Web 应用程序 [/LoggingMonitor] 似乎已经启动了一个名为 [Timer-1] 的线程,但未能停止它。这很可能造成内存泄漏。