예제 #1
0
 void onCancelled(SimpleTimerTask task) {
   synchronized (myTime2Task) {
     ArrayList<SimpleTimerTask> list = myTime2Task.get(task.getTargetTime());
     if (list != null) {
       list.remove(task);
       if (list.size() == 0) {
         myTime2Task.remove(task.getTargetTime());
       }
     }
   }
 }
예제 #2
0
  private void processNext() {
    Ref<ArrayList<SimpleTimerTask>> tasks = new Ref<ArrayList<SimpleTimerTask>>();

    synchronized (myTime2Task) {
      final long current = System.currentTimeMillis();

      final Iterator<Long> times = myTime2Task.keySet().iterator();

      if (times.hasNext()) {
        final Long time = times.next();
        tasks.set(myTime2Task.get(time));
        times.remove();
      }

      if (!times.hasNext()) {
        myNextScheduledTime = Long.MAX_VALUE;
        myNextProcessingTask = null;
      } else {
        Long nextEffectiveTime = null;
        while (times.hasNext()) {
          Long nextTime = times.next();
          if (nextTime <= current) {
            tasks.get().addAll(myTime2Task.get(nextTime));
            times.remove();
          } else {
            nextEffectiveTime = nextTime;
            break;
          }
        }

        if (nextEffectiveTime == null) {
          myNextProcessingTask = null;
          myNextScheduledTime = Long.MAX_VALUE;
        } else {
          scheduleNext(nextEffectiveTime - current, nextEffectiveTime);
        }
      }
    }

    final ArrayList<SimpleTimerTask> toRun = tasks.get();
    if (toRun != null) {
      for (SimpleTimerTask each : toRun) {
        try {
          each.run();
        } catch (ProcessCanceledException e) {
          throw e;
        } catch (Throwable e) {
          LOG.error(e);
        }
      }
    }
  }