Setting the timer task in Java is possible with the timer class.
one, delay execution
First, we define a class that takes a name called Timetask, and our timed task is executed in the main function of the class. The code is as follows:
Package test;
Import Java.util.Timer;
public class timetest{
public static void Main (string[] args) {
Timer timer = new timer ();
Timer.schedule (New Task (), 30 * 1000);
}
}
Explain the code above.
The above code implements a feature that performs a task after 30 seconds after the Timetest program is started. Very simple: First new a Timer object, and then call its schedule method, this method has four overloaded methods, here we use one,
public void Schedule (TimerTask task,long delay)
First of all, the first parameter
The first parameter is the task we are going to perform.
This is an TimerTask object, which is exactly the object of a class that implements TimerTask, because TimerTask is an abstract class. In the above code, the task is our own definition of the implementation of the TimerTask class, because it is in the same package, so there is no explicit import come in. The code for the task class is as follows
Package test;
Import Java.util.TimerTask;
public class Task extendsTimerTask{
public void Run () {
SYSTEM.OUT.PRINTLN ("Timed Task Execution");
}
}
Our task must implement the TimerTask method run, and the task to be performed is within the Run method, where we only let it line the console.
A second parameter
The second parameter is a long type value. This is the time of the delayIs how much time to perform scheduled tasks after the start of the program. The value of this long is the number of milliseconds, so in the previous program, the parameter value that was executed after one minute is 60 * 1000.
second, cyclic implementation
When setting up timed tasks, we often need to perform such tasks repeatedly, once every once in a while, and the method above is executed only once, so that the schedule method is another overloaded function
public void Schedule (TimerTask task,long Delay,long period)
The first two parameters don't have to say anything,the last parameter is the time of the interval, is a long type of milliseconds (it seems that Java is related to the time, with this long is irrelevant), such as we hope that the above task from the first execution, every minute of execution, the third parameter value assigned 60 * 1000 is OK.
Iii. specifying the time of execution
Since it is called a timed task, we certainly hope that we can specify the time specified by the task, obviously the above method is not used, because we do not know when the program starts to run, there is no way to determine how much delay. It doesn't matter, schedule four overloaded method is not finished yet. Use the following to be OK:
public void Schedule (TimerTask task,date time)
Like whatWe want the timed task to be executed on July 2, 2006 0:0, as long as the second parameter is passed to a Date object that is set to July 2, 2006 0:0.
In one case, it may be July 3, 2006 when our program is started, so that when the program starts, the scheduled task starts to execute.
Schedule the last overloaded method is
public void Schedule (TimerTask task,date Firsttime,long period)
Java timing (Looping) executes a method