Scheduled scheduling: Whenever a period of time, the program will be automatically executed, called scheduled scheduling.
If you want to use scheduled scheduling, you must ensure that the program is always running before you can, that is, the equivalent of scheduled scheduling is to start a new thread outside the program.
Timer and TimerTask two timer schedules.
、
If you want to schedule a timer class now, you need to use the TimerTask subclass.
According to some concepts, the completion of a scheduled scheduling program, every 2 seconds to print a time.
Import Java.util.TimerTask;
Import Java.util.Date;
Import Java.text.SimpleDateFormat;
Class MyTask extends timertask{//task scheduling classes inherit TimerTask
public void Run () {
SimpleDateFormat SDF = null;
SDF = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss.") SSS ");
SYSTEM.OUT.PRINTLN ("Current system time is:" + Sdf.format (new Date ());
}
};
Complete is a specific task operation class, the later scheduled scheduling is this kind of operation, the Main method is run () method.
Set up test classes and perform task scheduling.
Import Java.util.Timer;
public class testtask{
public static void Main (String args[]) {
Timer T = new timer (); Creating a Timer Class object
MyTask MyTask = new MyTask (); Defining tasks
T.schedule (mytask,1000,2000); Set task execution, start in 1 seconds, repeat every 2 seconds
}
}; Summarize:
This content is generally useful in the development of the web, because maintaining a container without shutting down can be done on a regular basis.