Java multithreaded Programming Core Technology (V) Timer timer use __ algorithm

Source: Internet
Author: User
Use of timer timers

In the JDK library, the timer class is primarily responsible for the ability to schedule tasks, i.e. to start a task at a specified time.
The primary role of the timer class is to set up a scheduled task, but the class that encapsulates the task does timertask the class. The code that executes the scheduled task is placed in the TimerTask subclass because TimerTask is an abstract class.

Method Schedule (TimerTask task,date time)
The purpose of this method is to perform a task once on a specified date.



Method Schedule (timertask,date Firsttime,long period)
The purpose of this method is to perform a task on a specified date only, at a specified interval, periodically with an infinite loop.
If the scheduled time is earlier than the current time, the task task is executed immediately.

The Cancel () method of the TimerTask class is used to purge itself from the task queue, and other tasks are unaffected.
The Cancel () method in the Timer class is used to empty all tasks in the task queue.

Method Schedule (timertask task,long delay)
The purpose of this method is to perform a timertask task after delaying the specified number of milliseconds based on the current time as the reference time.

Method Schedule (TimerTask task,long Delay,long period)
The purpose of this method is to use the current time as reference time, delay the specified number of milliseconds based on this time, and perform a task at an infinite number of intervals.

method Scheduleatfixedrate (TimerTask task,date firsttime,long period)
Method schedule and method Scheduleatfixedrate are executed sequentially, so do not consider non thread-safe situations, methods

The main difference between schedule and scheduleatfixedrate is only the case of no delay:
Using the Schedule method: If the time to perform a task is not delayed, the next task's execution reference time is calculated at the time of the "start" of the previous task.
Using the Scheduleatfixedrate method: If the time to perform a task is not delayed, the next task's execution time is calculated as a reference to the time of the last task's "end".

There is no difference in the delay, that is, if the time to perform the task is delayed, the next task's execution time is calculated by the time of the last task "end".

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.