private void mainLoop(){
  while (true) {
    try {
      TimerTask task;
      boolean taskFired;
synchronized (queue) {
        while (queue.isEmpty() && newTasksMayBeScheduled)         queue.wait();
        if (queue.isEmpty())         break;
        long currentTime, executionTime;
        task=queue.getMin();
synchronized (task.lock) {
          if (task.state == TimerTask.CANCELLED) {
            queue.removeMin();
            continue;
          }
          currentTime=System.currentTimeMillis();
          executionTime=task.nextExecutionTime;
          if (taskFired=(executionTime <= currentTime)) {
            if (task.period == 0) {
              queue.removeMin();
              task.state=TimerTask.EXECUTED;
            }
 else {
              queue.rescheduleMin(task.period < 0 ? currentTime - task.period : executionTime + task.period);
            }
          }
        }
        if (!taskFired)         queue.wait(executionTime - currentTime);
      }
      if (taskFired)       task.run();
    }
 catch (    InterruptedException e) {
    }
  }
}

private void mainLoop ( ) { while ( true ) { try { TimerTask task ; boolean taskFired ; synchronized ( queue ) { while ( queue . isEmpty ( ) && newTasksMayBeScheduled ) queue . wait ( ) ; if ( queue . isEmpty ( ) ) break ; long currentTime , executionTime ; task = queue . getMin ( ) ; synchronized ( task . lock ) { if ( task . state == TimerTask . CANCELLED ) { queue . removeMin ( ) ; continue ; } currentTime = System . currentTimeMillis ( ) ; executionTime = task . nextExecutionTime ; if ( taskFired = ( executionTime <= currentTime ) ) { if ( task . period == 0 ) { queue . removeMin ( ) ; task . state = TimerTask . EXECUTED ; } else { queue . rescheduleMin ( task . period < 0 ? currentTime - task . period : executionTime + task . period ) ; } } } if ( ! taskFired ) queue . wait ( executionTime - currentTime ) ; } if ( taskFired ) task . run ( ) ; } catch ( InterruptedException e ) { } } }
the main timer loop . ( see class comment . )
