Java Timed Task Scheduler timer entry details (ii)

Source: Internet
Author: User

In the previous article of the Java Timer Task Scheduler to get started in detail (a) article, the main introduction of the next timer, next we look at the timer of some common methods.

1, schedule () 4 kinds of usage.

First Type: Schedule (timertask task, Date time);

Task: Scheduled Tasks, time: specific execution times. The meaning of this function is to execute and execute the task once when time is equal to or greater than. The test content is as follows

Mytimertask.java's code is the same as the first one, and the Mytimer.java part of the code is as follows:

    

After running, the console output is as follows:

The first line prints the current time, and the second and third lines print the output of Run () in Mytimetask.java. This is at a glance

The second type: Schedule (timertask task, Date Firsttime, long period);

Firsttime: Refers to the time of the first execution of a task, period refers to the task execution cycle. This method says that the task executes once (the first time) when the time equals or firsttime, and then executes every period milliseconds. Say less nonsense, directly on the code.

Some of the test code for Mytimer.java is as follows:

    

The console section output is as follows:

    

The first line prints the current time, and the second and third lines print the first execution (after 3 seconds), followed by every two seconds.

The third type: Schedule (timertask task, lang delay);

Task: Scheduled Tasks, time: specific execution times. This function expresses the meaning of executing and executing a task at a time equal to or greater than the delay millisecond of the current time. The test content is as follows

Mytimertask.java's code is the same as the first one, and the Mytimer.java part of the code is as follows:

    

The console output is as follows

    

The first behavior of the current time, the last two behaviors after two seconds of the current time after the task executes the output.

Fourth type: Schedule (timertask task, long delay, long period); This means that after the delay millisecond of the current time, the task executes the first time, followed by every period millisecond.

The fourth kind believes that everybody unifies the second kind and the third kind can understand, here no longer gives the test code.

Two uses of 2.scheduleAtFixedRate ()

The first: scheduleatfixedrate (timertask task, Date Firsttime, long period), and the second usage of schedule is understood here to be the same, All indicate that the task executes once (the first time) at time equal to or firsttime, and then every period milliseconds thereafter. The test code is as follows:

Mytimertask.java's code is the same as the first one, and the Mytimer.java part of the code is as follows:

    

The console output is as follows:

    

The first line prints the current time, and the 23rd line prints the first time that the task executes after 3 seconds of the current time and then executes every 2 seconds.

The second type: scheduleatfixedrate (timertask task, long delay, long period), which indicates that the task executes the first time after a delay of milliseconds, followed by every period millisecond. The code is as follows:

Some of the code for Mytimer.java is as follows:

    

The console output is as follows:

    

    

Java Timed Task Scheduler timer entry details (ii)

Related Article

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.