论坛首页 入门技术论坛

用java写了一个定时器 为什么运行两天后就突然停了

浏览 24307 次
该帖已经被评为新手帖
作者 正文
   发表时间:2012-07-16  
是不是因为异常的原因!!!
   发表时间:2012-07-16  
把代码 贴出来  我们看看 才知道啊
0 请登录后投票
   发表时间:2012-07-16  
cwqcwqmax9 写道
把代码 贴出来  我们看看 才知道啊

package service;

import java.util.*;

import org.apache.log4j.Logger;
public class AutoMatic {

static Logger logger = Logger.getLogger ( AutoMatic.class.getName () ) ;
//程序启动一分钟后开始统计
static long timerDelay = 1000 * 60 * 1L;
//间隔时间,每2小时插一次
static long timerPeriod = 1000 * 60 * 60 *2L;

// {
// try{
// new Timer().schedule(new AutoMaticTimerTask(), timerDelay, timerPeriod);//timerDelay从现在开始延迟执行的时间 timerPeriod 延迟参数的时间单位
// System.out.println("服务已启动.服务启动一分钟后开始进行读取数据.");
// }catch (Exception e) {
// System.err.println("服务启动失败.");
// logger.error("#ERROR# :服务启动失败!", e);
// }
// }
class AutoMaticTimerTask extends TimerTask{
public void run(){

System.out.println("开始读取数据!");
try {
new ReportMinperiodService().selectBeforeTime();
ReportMinperiodService.ANALYSE_LOCKED=false;
System.out.println("ReportMinperiodService----->插值结束!!");
new ReportDayService().selectBeforeTime();
ReportDayService.ANALYSE_LOCKED=false;
System.out.println("ReportDayService----->插值结束!!");
new ReportMonthService().selectBeforeTime();
ReportMonthService.ANALYSE_LOCKED=false;
System.out.println("ReportMonthService----->插值结束!!");
new WrReportMinperiodService().selectBeforeTime();
WrReportMinperiodService.ANALYSE_LOCKED=false;
System.out.println("WrReportMinperiodService------>插值结束");
new WrReportDayService().selectBeforeTime();
WrReportDayService.ANALYSE_LOCKED=false;
System.out.println("WrReportDayService------>插值结束");
new WrReportMonthService().selectBeforeTime();
WrReportMonthService.ANALYSE_LOCKED=false;
System.out.println("WrReportMonthService------>插值结束");
new JzqEmsBaseService().selectBeforeTime();
JzqEmsBaseService.ANALYSE_LOCKED=false;
System.out.println("JzqEmsBaseService------>插值结束!!");
new JzqMidOutputService().selectBeforeTime();
JzqMidOutputService.ANALYSE_LOCKED=false;
System.out.println("JzqMidOutputService------>插值结束!!");


} catch (Exception e) {
e.printStackTrace();
logger.error("#ERROR# :插入错误!", e);
}

}

}

public static void main(String[] args) {
try{

new Timer().schedule(new AutoMatic().new AutoMaticTimerTask(), timerDelay, timerPeriod);//timerDelay从现在开始延迟执行的时间 timerPeriod 延迟参数的时间单位
System.out.println("服务已启动.一分钟后开始进行读取数据.");
}catch (Exception e) {
System.err.println("服务启动失败.");
logger.error("#ERROR# :服务启动失败!", e);
}
}
}
0 请登录后投票
   发表时间:2012-07-16  
log什么的你看了吗
0 请登录后投票
   发表时间:2012-07-16  
这格式不想看
0 请登录后投票
   发表时间:2012-07-16  
liubey 写道
log什么的你看了吗

看不出来!!
0 请登录后投票
   发表时间:2012-07-18  
这个很难讲。也许是task的run方法执行中阻塞了。
建议楼主重点参看log
0 请登录后投票
   发表时间:2012-07-18  
風一樣的男子 写道
这格式不想看

me too 哈哈
0 请登录后投票
   发表时间:2012-07-18  
hepeng19861212 写道
这个很难讲。也许是task的run方法执行中阻塞了。
建议楼主重点参看log


这个问题很蛋疼   找了两天也没解决     


最后写了个死循环   隔一段时间   sleep一下 !!
0 请登录后投票
   发表时间:2012-07-20  
new ReportMinperiodService().selectBeforeTime();
ReportMinperiodService.ANALYSE_LOCKED=false;
你用类的一个static变量做信号量吗,会不会有死锁情况出现,你在selectBeforeTime判断ANALYSE_LOCKED,如果你这个方法抛异常会出现什么问题,你的ReportMinperiodService结束了,但ANALYSE_LOCKED又可能还是true
0 请登录后投票
论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics