论坛首页 入门技术论坛

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

浏览 24219 次
该帖已经被评为新手帖
作者 正文
   发表时间:2012-07-29  
把代码的格式调一下,不然没有看下去的欲望了
0 请登录后投票
   发表时间:2012-08-09  
按代码格式贴出来,容易看。这样看有点晕。
0 请登录后投票
   发表时间:2012-09-26  
我来转成代码格式看看...
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-09-27  
Timer内部是用thread执行TimerTask的,当执行过程中遇到RuntimeException会退出任务的执行。
也就是说,在执行一段时间后,AutoMaticTimerTask中run方法中抛出异常。
如果你想追踪问题,最简单的方法在main方法最前面加如下代码:
Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() {
			
			@Override
			public void uncaughtException(Thread t, Throwable e) {
				System.out.println("thread " + t.getName() + " throws exception " + e.getMessage());
			}
		});
0 请登录后投票
   发表时间:2012-10-22  
jzq413026 写道
是不是因为异常的原因!!!

给您一个帖子的提示,自动重启线程,我猜你那个定时器应该是线程抛出异常,关闭了,java虚拟机关闭,所以定时器就停止了
http://sziitjiang.iteye.com/blog/1697053
0 请登录后投票
   发表时间:2012-10-30  
Rong_it 写道
風一樣的男子 写道
这格式不想看

me too 哈哈

me too.
0 请登录后投票
   发表时间:2012-10-30  
hekuilove 写道
Rong_it 写道
風一樣的男子 写道
这格式不想看

me too 哈哈

me too.

me too. 用Eclipse的格式化代码控飘过,哈哈
0 请登录后投票
   发表时间:2012-10-31  
sziitjiang 写道
hekuilove 写道
Rong_it 写道
風一樣的男子 写道
这格式不想看

me too 哈哈

me too.

me too. 用Eclipse的格式化代码控飘过,哈哈


团队协同开发禁止随意格式化代码
只允许选定自己修改的代码进行格式化
0 请登录后投票
   发表时间:2012-10-31  
風一樣的男子 写道
sziitjiang 写道
hekuilove 写道
Rong_it 写道
風一樣的男子 写道
这格式不想看

me too 哈哈

me too.

me too. 用Eclipse的格式化代码控飘过,哈哈


团队协同开发禁止随意格式化代码
只允许选定自己修改的代码进行格式化

你SVN用多了,哈哈,格式化之后再提交
0 请登录后投票
   发表时间:2012-10-31  
風一樣的男子 写道
sziitjiang 写道
hekuilove 写道
Rong_it 写道
風一樣的男子 写道
这格式不想看

me too 哈哈

me too.

me too. 用Eclipse的格式化代码控飘过,哈哈


团队协同开发禁止随意格式化代码
只允许选定自己修改的代码进行格式化

公司应该提供统一的格式化模板
0 请登录后投票
论坛首页 入门技术版

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