Timer.schedule (New MyTask (), long Time1,long timer2);
Today is a thorough understanding of this once let me headache of the method. Here's what I'd like to highlight:
The first parameter, which is the TimerTask class, is in the package: import Java.util.TimerTask. The consumer inherits the class and implements the public void run () method, because the TimerTask class implements the Runnable interface.
The second parameter means that when you call the method, the method will inevitably call the run () method in the TimerTask class TimerTask class, which is the difference between the two, which translates into Chinese meaning that, after the user calls the schedule () method, It takes such a long time to execute the run () method for the first time.
The third parameter means that after the first call, the run () method is called once every few hours from the second start.
[Attached:]
The most important step in integrating the internal Office system with the external Web site is to read the data from the OA system and generate the final static page based on the site template. Here you need a timed task, the execution of the loop.
When a technician writes a timed task, it assumes that Timer.schedule (timertask task, long delay) is a repeating task. After running the program found that only run once, always feel that the code in the task is a problem, it took a long time to debug the code has no results.
Read through the Java API and discover:
Note Schedule (timertask task, long delay): Schedules the specified task for execution after the specified delay. The main idea is to execute the task after delaying delay of milliseconds. No mention of repeated executions
Schedule (timertask task, long delay, long period) Comment: schedules the specified task for repeated fixed-delay execution, Begi Nning after the specified delay. The main idea is to repeat the execution of the task after the delay time delay of milliseconds, the period is period milliseconds.
The problem is clear schedule (timertask task, long delay) is executed only once, schedule (timertask task, long delay, long period) is the repeated execution. The key problem is that programmers mistakenly think that schedule is repeated execution, without careful study of the API, on the one hand is not enough English ability, the process of browsing the API can not quickly understand the meaning.
How to use the schedule () method of the timer correctly?