In the process of development, Java is sometimes required to implement job functions, timed to open or repeat a task for a period of time.
This article will write a simple Java job function
1. First you need to write the job's execution content. The job class needs to inherit the Java.util.TimerTask class
Import Java.util.TimerTask;
public class Testjob extends timertask{
@Override public
void Run () {
System.out.println ("Perform today's Tasks");
}
}
Inheritance needs to implement the run () method, open up a bit like threading method, in fact, that is, open a thread to execute the job content, timertask implementation of the Runnable interface.
2. After the task class is complete, you need to create a timer to monitor the time that you set up the task, to a certain time, or to perform the task at every time.
Timer class can complete the function of timing, the timer class Shedule can perform job tasks.
Timer timer=new timer ();
Timer.schedule (TimerTask task,date time);
Timer.schedule (timertask task,long delay);
Timer.schedule (TimerTask task,date firsttime,long period);
Timer.schedule (TimerTask task,long delay,long period);
Test: Testing the program
Implementation starts at zero and performs tasks every day
public static void Main (string[] args) {
Timer timer = new timer ();
Calendar date = Calendar.getinstance ();
Date.set (calendar.hour_of_day, 0);
Date.set (calendar.minute, 0);
Date.set (Calendar.second, 0);
Date.set (Calendar.millisecond, 0);
Timer.schedule (New Testjob (), date, 1000*60*60*24);
}