调用servlet时如何终止一个正在运行的timerTask
开发一个java程序,发现在调用一个servlet时因此时tomcat正在运行一个timerTask而要等待该定时器运行结束才能有调用结果,在显示页面就会有等待长时间现象。问题是:
如何判断一个timerTask是否在运行,如在运行就终止该线程(timerTask),而直接运行该servlet
listener如下:
public class reportListner implements servletContextListener{
private Timer timer=null;
public void contextDestroyed(ServletContextEvent arg0){
timer.cancel();
}
public void contextInitialized(ServletContextEvent arg0){
timer=new Timer(true);
timer.scheduleAtFixedRate(new ReportTask(),20*1000,20*60*1000);
}
}
task如下:
public class ReportTask extends TimerTask{
public void run(){
ReportBeanCl rbc=new ReportBeanCl();
rbc.updateReport();
}
}
servlet如下:
public class ReportServlet extends HttpServlet{
public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
.....
}
public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
this.doGet(request,response);
}
}