java - TimerTask is not all executed successfully
世界只因有你
世界只因有你 2017-06-28 09:24:54
0
1
841

I created a TimerManager class to allow the system to execute the corresponding four tasks after it is running. However, only two of the four tasks were successfully executed, namely timer and timer3, and the following two tasks, timer2 and timer4, were not executed. I can't figure it out, so I came to sf and asked you to help me find out what the problem is. The following is the code of TimerManager:

public class TimerManager { @Resource RemoteControlController remoteControlController; @Resource ManagementStationService managementStationService; @Resource ControllerStatusController controllerStatusController; // 时间间隔 private static final long PERIOD_DAY = 24 * 60 * 60 * 1000; public static final long PERIOD_DAY2 = 60 * 60 * 1000; private static final long PERIOD_DAY3 = 60 * 60 * 1000; private static final int START_TIME = 1; private static final int START_TIME2 = 0; private Logger log = Logger.getLogger("ServerInfo"); public void initTimerManager() { Calendar calendar = Calendar.getInstance(); Calendar calendar2 = Calendar.getInstance(); /*** 定制每日1:00执行方法 ***/ calendar.set(Calendar.HOUR_OF_DAY, START_TIME); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); /*** 定制每日0:00执行方法 ***/ calendar2.set(Calendar.HOUR_OF_DAY, START_TIME2); calendar2.set(Calendar.MINUTE, 0); calendar2.set(Calendar.SECOND, 0); Date date = calendar.getTime(); //第一次执行定时任务的时间 ,date重启不执行 Date date2 = calendar.getTime();//date2、3重启执行 Date date3 = calendar2.getTime(); // 如果第一次执行定时任务的时间 小于 当前的时间 // 此时要在第一次执行定时任务的时间加一天,以便此任务在下个时间点执行。如果不加一天,任务会立即执行。 if (date.before(new Date())) { date = this.addDay(date, 1); } Timer timer = new Timer(); Timer timer2 = new Timer(); Timer timer3 = new Timer(); Timer timer4 = new Timer(); UpdateDailyEletricPowerTimerTask task = new UpdateDailyEletricPowerTimerTask(); UpdateLampStatusTimerTask task2 = new UpdateLampStatusTimerTask(); UpdateCurrentEletricDataTimerTask task3 = new UpdateCurrentEletricDataTimerTask(); UpdateCurrentControllerStatusTimerTask task4 = new UpdateCurrentControllerStatusTimerTask(); // 安排指定的任务在指定的时间开始进行重复的固定延迟执行。 timer.schedule(task, date, PERIOD_DAY); timer3.schedule(task3, date3, PERIOD_DAY2); timer4.schedule(task4, date3, PERIOD_DAY3);//先更新路由状态 timer2.schedule(task2, date2, PERIOD_DAY);//再更新灯状态 } // 增加或减少天数 public Date addDay(Date date, int num) { Calendar startDT = Calendar.getInstance(); startDT.setTime(date); startDT.add(Calendar.DAY_OF_MONTH, num); return startDT.getTime(); } public class UpdateCurrentEletricDataTimerTask extends TimerTask { @Override public void run() { try { // 在这里写你要执行的内容 /** * 查询实时功率等数据 */ log.info("-------------UpdateCurrentEletricDataTimerTask正在执行--------------"); remoteControlController.currentElectricDataCollecting(); log.info("-------------UpdateCurrentEletricDataTimerTask执行完毕--------------"); } catch (Exception e) { log.info("-------------UpdateCurrentEletricDataTimerTask解析信息发生异常--------------"); } } }
public class UpdateCurrentControllerStatusTimerTask extends TimerTask { @Override public void run() { try { // 在这里写你要执行的内容 /** * 查询实时功率等数据 */ log.info("-------------UpdateCurrentControllerStatusTimerTask正在执行--------------"); remoteControlController.readRouterStatus(); log.info("-------------UpdateCurrentControllerStatusTimerTask执行完毕--------------"); } catch (Exception e) { log.info("-------------UpdateCurrentControllerStatusTimerTask解析信息发生异常--------------"); } } } public class UpdateDailyEletricPowerTimerTask extends TimerTask { @Override public void run() { try { // 在这里写你要执行的内容 /** * 查询前昨两天日冻结正向有功总电量 */ log.info("-------------UpdateDailyEletricPowerTimerTask正在执行--------------"); remoteControlController.dailyPositiveElectricPowerCollecting(); log.info("-------------UpdateDailyEletricPowerTimerTask执行完毕--------------"); } catch (Exception e) { log.info("-------------UpdateDailyEletricPowerTimerTask解析信息发生异常--------------"); } } } public class UpdateLampStatusTimerTask extends TimerTask { @Override public void run() { try { // 在这里写你要执行的内容 /** * 更新全部灯具状态 */ log.info("-------------UpdateLampStatusTimerTask正在执行--------------"); List mlist = managementStationService.getManagementStationList(); int msize = mlist.size(); log.info("**********UpdateLampStatusTimerTask获取的管理所长度为"+msize); if(msize > 0){ String[] arr = new String[msize]; for(int i = 0; i < msize; i++) arr[i] = String.valueOf(mlist.get(i).getMid()); boolean realTime = false; controllerStatusController.UpdateControllerStatus(arr,realTime); } log.info("-------------UpdateLampStatusTimerTask执行完毕-------------"); } catch (Exception e) { log.info("-------------UpdateLampStatusTimerTask解析信息发生异常--------------"); } } } }
世界只因有你
世界只因有你

reply all (1)
仅有的幸福

I have never used Timer and am not going to give advice on how to debug this program. I recommend using a mature framework like quartz for this kind of thing. If you must design it yourself, Timer is not the best choice. The standard idea should be:

  1. Create a daemon thread to manage the cycle of each scheduled task;

  2. When the execution time of a scheduled task is up, the daemon thread opens another thread to execute the task;

  3. If the execution time of a task exceeds the cycle, the next execution time will be postponed to the next cycle.

    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!