Timer Timing System

Source: Internet
Author: User

Timer

Timer is the most commonly used tool class when you need to execute simple tasks in a scheduled manner. There are two ways to use Timer to schedule the Real-Time Workers of TimerTask to execute the task,
One is to run the task once at a specified time,
Another method is to periodically execute tasks from a specified time.

 

The following is a simple example of Timer, which executes a specific operation doWork every 10 seconds.

Timer timer = new Timer ();

TimerTask task = new TimerTask ()
{

Public void run ()
{

DoWork ();

}

};

Timer. schedule (task, interval L, interval L );

As you can see, the specific task is implemented by the TimerTask sub-class, and Timer is responsible for managing and executing TimerTask.

Call Methods

Use of Timer
Different Timer interfaces are required in different scenarios. As mentioned above, it mainly distinguishes between two situations.

1) execute the task only once at the specified time

-Public void schedule (TimerTask task, long delay)
2) The task is executed cyclically from the specified time until the task is canceled by cancel. And

-Public void schedule (timertask task, date time)
There are two types:

2.1) One method is to calculate the execution time based on the execution time of the last task, which can be called the relative time method. For example, if the first task is executed at 1 minute 10 seconds and the cycle is 5 seconds, because the system is busy (such as garbage collection and virtual memory switching), there is no chance of execution at 1 minute 15 seconds, the second task is not executed until 1 minute 16 seconds, so the execution time of the first task will be 1 minute 21 seconds, with an offset of 1 second.

-Public void schedule (timertask task, long delay, long period)

-Public void schedule (timertask task, date firsttime, long period)

 

2.2) the other is the absolute time method. Based on the user's design start time, the nth execution time is "Start Time + N * cycle time ". For example, in the above case, although the system is busy and the second execution time is pushed back for 1 second, the 3rd time point should still be 1 minute 20 seconds.

-Public void scheduleatfixedrate (timertask task, long delay, long period)

-Public void scheduleatfixedrate (timertask task, date firsttime ,&

 

2)
Let's look at this example.
Listener
Actionlistener taskpolicmer = new actionlistener (){

Public void actionreceivmed (actionevent EVT ){

// Refresh the page.

Repaint ();

}

};

// Timer object. The value 1000 indicates 1 second, indicating that the method in the listener is executed once every second.

Timer = new timer (1000, taskreceivmer );

// Start the timer

Timer. Start ();

Public void start () starts timer so that it starts to send Action events to its listener

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.