コード例 #1
0
 public void startTask(String taskCode) {
   Task task = taskPool.get(taskCode);
   if (task.getThread() == null) {
     doOneTask0(task);
   } else {
     if (task.getThread().isAlive()) {}
   }
 }
コード例 #2
0
  // 处理某个任务
  private void doOneTask0(Task task) {
    logger.error("doOneTask0...");

    try {
      Runnable runnable = (Runnable) Class.forName(task.getClz()).newInstance();
      if (runnable != null) {
        Thread thread = new Thread(runnable);
        task.setThread(thread);

        // 计算出此任务下一次的执行时间点
        String nextWorkTime = null;
        if (task.getUnit().equalsIgnoreCase("DAYS")) {
          nextWorkTime =
              sdf.format(DateUtils.addDays(new Date(), Integer.valueOf(task.getSleep())));
        } else if (task.getUnit().equalsIgnoreCase("HOURS")) {
          nextWorkTime =
              sdf.format(DateUtils.addHours(new Date(), Integer.valueOf(task.getSleep())));
        } else if (task.getUnit().equalsIgnoreCase("MINUTES")) {
          nextWorkTime =
              sdf.format(DateUtils.addMinutes(new Date(), Integer.valueOf(task.getSleep())));
        } else if (task.getUnit().equalsIgnoreCase("SECONDS")) {
          nextWorkTime =
              sdf.format(DateUtils.addSeconds(new Date(), Integer.valueOf(task.getSleep())));
        } else if (task.getUnit().equalsIgnoreCase("MILLISECONDS")) {
          nextWorkTime =
              sdf.format(DateUtils.addMilliseconds(new Date(), Integer.valueOf(task.getSleep())));
        }
        task.setNextWorkTime(nextWorkTime);
        task.setCurrentStatus(Task.task_currentStatus_wait);

        thread.setDaemon(true);
        thread.setName(task.getCode());
        thread.start();
      }
    } catch (ClassNotFoundException e) {
      e.printStackTrace();
    } catch (InstantiationException e) {
      e.printStackTrace();
    } catch (IllegalAccessException e) {
      e.printStackTrace();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }