日期:2014-05-20  浏览次数:20788 次

Time() 线程会不会没有处理完就销毁?异常也没有报。求救!!!
最近开放赌场的项目,好郁闷,程序找不到日志,求求大哥哥解救一下啊,先提前谢谢啦。。

问题是这样的:

  我启动一个定时程序,在run方法里调用我的方法currToHis(),

  然后有可能出现异常了,但是异常信息没有打印,是不是这个线程自动销毁了。


代码:

 /**
  * 2012-03-15
  * @param DBparam
  * @throws Exception 
  */
  public void currToHis(String DBparam,int s) {
  try {
  DBInstance(DBparam).startTransaction(Connection.TRANSACTION_SERIALIZABLE);
DBInstance(DBparam).insert("currtoHis", null);
System.out.println("Log:========this is insert opertation=====");
DBInstance(DBparam).delete("deleteCurr", null);
System.out.println("Log:========this is delete opertation=====");
DBInstance(DBparam).commitTransaction();
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
DBInstance(DBparam).endTransaction();
} catch (SQLException e) {
e.printStackTrace();
}
}
  }



定時程序:

@Override
public void contextInitialized(ServletContextEvent arg0) {
// TODO Auto-generated method stub
timer = new Timer();
timeSimple = new TimeSimple();

SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String now = sf.format(new Date());
String result = now.substring(0,11)+time;
System.out.println(now+"~~~~~~~~~~~~~~~~~~~"+result);
System.out.println(now.substring(13));
try {
long target = sf.parse(result).getTime()-new Date().getTime();
System.out.print("the method will be start after "+target);
timer.schedule(timeSimple, target<0?target+24*60*60*1000:target, 24*60*60*1000);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

日誌信息:

2012-04-18 10:00:00,102 DEBUG PreparedStatement:23 - {pstm-103295} PreparedStatement: insert into HisSummary select sycom, TableNumber, TableAreaType, GameDay, OccurenceTime, WinLossAmount, Status, RegDate, CurrFlag, Turnover from CurrTabSumry WHERE GameDay<>(select max(GameDay) GameDay from CurrTabSumry (nolock) )  
2012-04-18 10:00:00,118 DEBUG PreparedStatement:23 - {pstm-103295} Parameters: []
2012-04-18 10:00:00,118 DEBUG PreparedStatement:23 - {pstm-103295} Types: []
2012-04-18 10:00:05~~~~~~~~~~~~~~~~~~~2012-04-18 10:00:00
:00:05
the method will be start after -59622012-04-18 10:00:06,368 INFO CheckSessionOutFilter:53 - /js/LoadSummary.jsp
2012-04-18 10:00:06,368 INFO CheckSessionOutFilter:54 - loginName=05897;gameday=2012-04-18;
sqlMapClient initing.....
2012-04-18 10:00:07,196 DEBUG Connection:23 - {conn-100000} Connection
2012-04-18 10:00:07,259 DEBUG PreparedStatement:23 - {pstm-100001} PreparedStatement: select ID,sycom,companyname ,description,propertyVal from property order by sycom  
2012-04-18 10:00:07,259 DEBUG PreparedStatement:23 - {pstm-100001} Parameters: []
2012-04-18 10:00:07,274 DEBUG PreparedStatement:23 - {pstm-100001} Types: []
2012-04-18 10:00:07,290 DEBUG ResultSet:23 - {rset-100002} ResultSet

------解决方案--------------------
Timer里只有一个线程,在不同的平台表现不一样

在win下,我测试过run方法抛出了异常,timer不会终止

但在aix下,确实会终止掉

所以,run最好是不要抛出异常
---------
推荐使用Executors.newScheduledThreadPool(...).shedule...,这种方式run抛出异常不会导致定时任务终止
------解决方案--------------------
探讨

谢谢你回复啊,但是我还不确定是不是因为这个原因,我还有疑问就是:


上面的那个程序出现异常,为什么捕获不到呢?

还有这个定时任务他还会执行,就是每天10:00做这个任务,今天失败了,明天有可能成功,后天又不定了。

其实我想要的结果是想把异常信息得到,查看具体的原因,比如sql 错误等。

------解决方案--------------------