问题描述
最近小虾米我配置了个Tomcat的网站,其中设置了一个Listener,放置了两个Timer,其中一个timer_ExchangeRate一直很正常,但是timer_FondValue却会不定时死掉,不再执行任务。(timer_FondValue运行程序的时间会长于另一个TIMER)这究竟是什么原因导致的呢?有没有解决方案啊?Listener代码如下importjava.util.Timer;importjava.util.TimerTask;importjavax.servlet.ServletContextEvent;importjavax.servlet.ServletContextListener;importcom.test.GetExchangeRate;importcom.test.GetFondValue;//importcom.publisherTest.GetFond;publicclassGetSourceTimerimplementsServletContextListener{publicGetSourceTimer(){}publiclongrefresh_ExchangeRate=60000;publiclongrefresh_FondValue=180000;/***无延迟*/publicstaticfinallongNO_DELAY=0;/***定时器*/privateTimertimer_ExchangeRate;privateTimertimer_FondValue;//publicstaticinti=0;/***在Web应用启动时初始化任务*/publicvoidcontextInitialized(ServletContextEventevent){//定义定时器//i++;timer_ExchangeRate=newTimer("汇率刷新",true);timer_FondValue=newTimer("净值刷新",true);timer_ExchangeRate.schedule(newmyTask(),NO_DELAY,refresh_ExchangeRate);timer_FondValue.schedule(newFondTask(),NO_DELAY,refresh_FondValue);}/***在Web应用结束时停止任务*/publicvoidcontextDestroyed(ServletContextEventevent){timer_ExchangeRate.cancel();//定时器销毁timer_FondValue.cancel();}classmyTaskextendsTimerTask{privatebooleanisRunning=false;//privatestaticLoglog=LogFactory.getLog(BackUpTableTask.class);publicvoidrun(){if(!isRunning){isRunning=true;//更新汇率信息GetExchangeRategetrate=newGetExchangeRate();if(getrate.CheckIsNull()){getrate.iniExchangeRate();}else{if(!getrate.CheckExchangeTime()){getrate.UpadateExchangeRate();}}isRunning=false;}else{this.cancel();}}}classFondTaskextendsTimerTask{privatebooleanisRunning=false;//privatestaticLoglog=LogFactory.getLog(BackUpTableTask.class);publicvoidrun(){if(!isRunning){isRunning=true;//更新汇率信息GetFondValuegetfond=newGetFondValue();if(getfond.CheckIsNull()){getfond.iniFondValue();}else{if(!getfond.CheckExchangeTime()){getfond.reload();}}isRunning=false;}else{this.cancel();}}}}跪求各位大大了!
解决方案
解决方案二:
有没有人知道啊?急啊!
解决方案三:
FondTask在执行时,可能执行的时间比较长,如果没有执行完的话,你做了取消当前任务的操作isRunning=false;}else{this.cancel();}所以,以后不再执行任务。