Example #1
0
 public int purge() {
   if (tasks.isEmpty()) {
     return 0;
   }
   // callers are synchronized
   tasks.deletedCancelledNumber = 0;
   tasks.deleteIfCancelled();
   return tasks.deletedCancelledNumber;
 }
Example #2
0
 private void insertTask(TimerTask newTask) {
   // callers are synchronized
   tasks.insert(newTask);
   this.notify();
 }
Example #3
0
 /** Cancels timer. */
 public synchronized void cancel() {
   cancelled = true;
   tasks.reset();
   this.notify();
 }
Example #4
0
    /** This method will be launched on separate thread for each Timer object. */
    @Override
    public void run() {
      while (true) {
        final TimerTask task;
        synchronized (this) {
          // need to check cancelled inside the synchronized block
          if (cancelled) {
            return;
          }
          if (tasks.isEmpty()) {
            if (finished) {
              return;
            }
            // no tasks scheduled -- sleep until any task appear
            try {
              this.wait();
            } catch (InterruptedException ignored) {
            }
            continue;
          }

          long currentTime = System.currentTimeMillis();

          task = tasks.minimum();
          long timeToSleep;

          synchronized (task.lock) {
            if (task.cancelled) {
              tasks.delete(0);
              continue;
            }

            // check the time to sleep for the first task scheduled
            timeToSleep = task.when - currentTime;
          }

          if (timeToSleep > 0) {
            // sleep!
            try {
              this.wait(timeToSleep);
            } catch (InterruptedException ignored) {
            }
            continue;
          }

          // no sleep is necessary before launching the task

          synchronized (task.lock) {
            int pos = 0;
            if (tasks.minimum().when != task.when) {
              pos = tasks.getTask(task);
            }
            if (task.cancelled) {
              tasks.delete(tasks.getTask(task));
              continue;
            }

            // set time to schedule
            task.setScheduledTime(task.when);

            // remove task from queue
            tasks.delete(pos);

            // set when the next task should be launched
            if (task.period >= 0) {
              // this is a repeating task,
              if (task.fixedRate) {
                // task is scheduled at fixed rate
                task.when = task.when + task.period;
              } else {
                // task is scheduled at fixed delay
                task.when = System.currentTimeMillis() + task.period;
              }

              // insert this task into queue
              insertTask(task);
            } else {
              task.when = 0;
            }
          }
        }

        boolean taskCompletedNormally = false;
        try {
          task.run();
          taskCompletedNormally = true;
        } finally {
          if (!taskCompletedNormally) {
            synchronized (this) {
              cancelled = true;
            }
          }
        }
      }
    }