/** * Terminates this timer, discarding any currently scheduled tasks. Does not interfere with a * currently executing task (if it exists). Once a timer has been terminated, its execution thread * terminates gracefully, and no more tasks may be scheduled on it. * * <p>Note that calling this method from within the run method of a timer task that was invoked by * this timer absolutely guarantees that the ongoing task execution is the last task execution * that will ever be performed by this timer. * * <p>This method may be called repeatedly; the second and subsequent calls have no effect. */ public void cancel() { synchronized (queue) { queue.newTasksMayBeScheduled = false; queue.clear(); queue.notify(); // In case queue was already empty. } }
/** start the main processing loop. */ public void run() { try { mainLoop(); /* * If mainLoop returns then thread timed out with no events * in the queue. The thread will quietly be restarted in sched() * when the next TimerTask is queued. */ } catch (Throwable t) { // Someone killed this Thread, behave as if Timer cancelled synchronized (queue) { queue.newTasksMayBeScheduled = false; queue.clear(); // Eliminate obsolete references } } }