Task SchedulingCode: reminder.javaimport java.util.Timer; import java.util.TimerTask; / *** Simple demo that uses java.util.Timer to schedule a task * to execute once 5 seconds have passed * / public class Reminder. {Timer timer; public Reminder (int seconds) {timer = new Timer (); timer.schedule (new RemindTask (), seconds * 1000);} class RemindTask extends TimerTask {public void run () {System.out.println ( "Time's Up!"); Timer.Cancel (); // Terminate the Timer Thread}} public static void main (string args []) {new recnder (5); system.out.println ("task scheduled.") }}} Stopping Timer Threadsthere Are Four Ways That You CAN Stop A Timer:
INVOKE CANCEL on the Timer. You Can do this from anywhere in the program only threads left in the program are daemon threads, the program exits. After all the timer Zha scheduled tasks have finished executing, remove all references to the Timer object. Eventually, the timer Zha thread will terminate. Invoke the System.exit method, which makes the entire program (and all its threads) exit Performing a Task Repeatedly This can be accomplished by changing a different timer.schedule ();. Here are all the schedule APIs that can be used to performe a task repeatedlyschedule (TimerTask task, long delay, long period) schedule (TimerTask task, Date time, long period) scheduleAtFixedRate (TimerTask task, long delay, long period) scheduleAtFixedRate (TimerTask task, Date firstTime, long period) Implementing the Runnable Interface You have now seen two ways to provide THE Run Meth OD.
Subclass the Thread class and override the run method. See the SimpleThread class described in the section Subclassing Thread and Overriding run. Provide a class that implements the Runnable interface and therefore implements the run method. In this case, a Runnable object provides the run method to the thread. See the Clock applet in the preceding section.There are good reasons for choosing either of these options over the other. However, for most cases, including that of the Clock applet, if your class must subclass some other class (the Most Common Example Being Applet), You Should Use Runnable.Import java.awt.graphchics; import java.util. *;
Import java.text.dateFormat;
IMPORT JAVA.Applet.Applet;
Public Class Clock Extends Applet Implements Runnable {
Private thread clockthread = NULL;
Public void start () {
IF (clockthread == null) {
ClockThread = New Thread (this, "clock");
Clockthread.start ();
}
}
Public void run () {
Thread mythread = thread.currentthread ();
While (clockthread == mythread) {
Repaint ();
Try {
Thread.sleep (1000);
} catch (interruptedexception e) {
// The VM Doesn 抰 Want US to Sleep Anymore,
// SO Get Back to Work
}
}
}
Public void paint (graphics g) {
// Get the time and communication it to a date
Calendar Cal = Calendar.getInstance ();
Date Date = Cal.getTime ();
// Format IT and Display IT
DateFormat DateFormatter =
DateFormat.gettimeInstance ();
g.drawstring (Dateformatter.Format (Date), 5, 10);
}
// Overrides Applet 抯 Stop Method, Not thread 抯
Public void stop () {
Clockthread = NULL;
}
}