Java Reference
In-Depth Information
Example 4−5: Timer.java (continued)
timer = new TimerThread(isDaemon); // TimerThread is defined below
timer.start();
// Start the thread running
}
/** Stop the timer thread, and discard all scheduled tasks */
public void cancel() {
synchronized(tasks) { // Only one thread at a time!
timer.pleaseStop(); // Set a flag asking the thread to stop
tasks.clear();
// Discard all tasks
tasks.notify();
// Wake up the thread if it is in wait().
}
}
/** Schedule a single execution after delay milliseconds */
public void schedule(TimerTask task, long delay) {
task.schedule(System.currentTimeMillis() + delay, 0, false);
schedule(task);
}
/** Schedule a single execution at the specified time */
public void schedule(TimerTask task, Date time) {
task.schedule(time.getTime(), 0, false);
schedule(task);
}
/** Schedule a periodic execution starting at the specified time */
public void schedule(TimerTask task, Date firstTime, long period) {
task.schedule(firstTime.getTime(), period, false);
schedule(task);
}
/** Schedule a periodic execution starting after the specified delay */
public void schedule(TimerTask task, long delay, long period) {
task.schedule(System.currentTimeMillis() + delay, period, false);
schedule(task);
}
/**
* Schedule a periodic execution starting after the specified delay.
* Schedule fixed-rate executions period ms after the start of the last.
* Instead of fixed-interval executions measured from the end of the last.
**/
public void scheduleAtFixedRate(TimerTask task, long delay, long period) {
task.schedule(System.currentTimeMillis() + delay, period, true);
schedule(task);
}
/** Schedule a periodic execution starting after the specified time */
public void scheduleAtFixedRate(TimerTask task, Date firstTime,
long period)
{
task.schedule(firstTime.getTime(), period, true);
schedule(task);
}
// This internal method adds a task to the sorted set of tasks
void schedule(TimerTask task) {
Search WWH ::




Custom Search