How to use the schedule () method of the timer correctly?

Source: Internet
Author: User

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?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.