Schedule and Scheduleatfixedrate in Java timer

Source: Internet
Author: User

First, let's look at a few examples

Import Java.util.Timer;
Import Java.util.TimerTask;
Import Java.util.Date;

/**
* @author Vincent
*/
public class Timertest {

public static void Main (string[] args) {
Timer T = new timer ();

Perform the TimerTask task after 5 seconds
T.schedule (New TimerTask () {
public void Run ()
{SYSTEM.OUT.PRINTLN ("This are task you Do1");}
},5*1000);


Perform timertask tasks after a specific time specified by date
Date D1 = new Date (System.currenttimemillis () +1000);
T.schedule (New TimerTask () {
public void Run ()
{SYSTEM.OUT.PRINTLN ("This are task you Do2");}
},D1);


Performs a timertask task once every 1 seconds after a specific time specified by date

Schedule (TimerTask task,
                     Date Firsttime,
                     long period)
Date D2 = new Date (System.currenttimemillis () +1000);
T.schedule (New TimerTask () {
public void Run ()
{SYSTEM.OUT.PRINTLN ("This are task you Do3");}
},d2,1*1000);

Schedules the specified task to begin repeating fixed-delay execution at a specified time. followed by an approximate fixed interval (separated by the specified cycle).

In fixed-delay execution, each execution is scheduled based on the actual execution time of the previous execution. If a execution is deferred for any reason (such as garbage collection or other background activity), subsequent execution will also be deferred. In a long-running operation, the frequency of execution is typically slightly slower than the reciprocal of the specified cycle (assuming that object.wait (long) relies on the correct system clock).

Fixed-delay execution applies to repetitive execution activities that require a "smooth" run. In other words, it applies to activities that are more important to keep the frequency accurate in a short run than in the long run. This includes most animated tasks, such as cursors flashing at fixed intervals. This also includes fixed activities that are performed in response to human activity, such as automatically repeating characters as you press and hold the key.


Perform the TimerTask task once every 1 seconds after 3 seconds
T.schedule (New TimerTask () {
public void Run ()
{SYSTEM.OUT.PRINTLN ("This are task you Do4");}
},3*1000,1*1000);

After 3 seconds, absolutely every 2 seconds to perform the TimerTask task once

T.scheduleatfixedrate (New TimerTask () {
public void Run ()
{SYSTEM.OUT.PRINTLN ("This are task you Do6");}
},3*1000,2*1000);
}

Performs a timertask task once every period time after a specific time specified by date

scheduleatfixedrate (TimerTask task,
                                Date Firsttime,
                                long period)
Schedules the specified task to begin repeating fixed-rate execution at a specified time. followed by an approximate fixed interval (separated by the specified cycle).

In fixed-rate execution, each execution is scheduled relative to the scheduled initial execution time. If an execution is deferred for any reason (such as garbage collection or other background activity), two or more executions will occur quickly and continuously, so that subsequent execution can catch up. In the long run, the frequency of execution is exactly the reciprocal of the specified cycle (assuming that object.wait (long) relies on the exact system clock).

Fixed-rate execution applies to repetitive execution activities that are sensitive to absolute time, such as clock timekeeping hourly, or scheduled maintenance activities at specific times of day. It also applies to repetitive activities that are important to complete a fixed number of execution times, such as a countdown timer, which ticks every second for 10 seconds. Finally, fixed-rate execution applies to timer tasks that schedule repeated executions, which must be synchronized with each other.


The difference between schedule and scheduleatfixedrate is that if you specify that the time to start execution precedes the current system run time, Scheduleatfixedrate will take the time that has elapsed as a cycle, and schedule will not count the time of the past.

Like what

SimpleDateFormat ftime = new SimpleDateFormat ("Yyyy/mm/dd HH:mm:ss");
Date D1 = Ftime.parse ("2005/12/30 14:10:00");

T.scheduleatfixedrate (New TimerTask () {
public void Run ()
{
System.out.println ("This is task for you Do6");
}
},d1,3*60*1000);

The interval is 3 minutes, the specified start time is 2005/12/30 14:10:00, and if I execute this program at 14:17:00, it will print 3 times at once

This is task for you do6//14:10
This is task for you do6//14:13
This is task for you do6//14:16

And note that the next execution is in 14:19 instead of 14:20. This means that the timer starts at the specified start time, not from the execution time.

But if you use the Schedule method, the time interval is 3 minutes, the specified start time is 2005/12/30 14:10:00, then the program is executed at 14:17:00, then the program is executed immediately. And the next execution time is 14:20, not the cycle starting from 14:10 (14:19).

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.