It is now used in the project to check whether the file is updated in a timely way. The timer is right here.
The usage is very simple, new a timer, and then write a timertask subclass.
The code is as follows:
Package comz.autoupdatefile;
Import Java.util.Timer;
Import Java.util.TimerTask;
public class M {public
static void Main (string[] args) {
//TODO todo.generated by zoer
Timer timer = new Tim ER ();
Timer.schedule (New MyTask (), 1000, Watts);
}
Class MyTask extends TimerTask {
@Override public
void Run () {
System.out.println ("dddd");
}
This allows the execution of MyTask to begin after 1 seconds, once every two seconds.
Of course, the timer's function can also construct the thread by itself, and then use sleep in the thread to simulate stopping for a while before performing an action.
In fact, look at the source of TimerTask can immediately know, TimerTask is the realization of the Runnable interface. That is, a timer is used to perform an operation over a period of time, but also through a thread.