Experiments of Timer.scheduleatfixedrate and Timer.schedule

Source: Internet
Author: User

Base code:

Calendar currenttime = Calendar.getinstance ();

Currenttime.settime (New Date ());

int currenthour = Currenttime.get (Calendar.second);

Currenttime.set (Calendar.second, Currenthour + 20);//First (greater than 20 seconds): Currenthour + 20; second (less than 20 seconds): Currenthour- 20; third (equals current): Currenthour
Currenttime.set (Calendar.millisecond, 0);

Date nexthour = Currenttime.gettime ();

Explain:

Nexthour

1*60*1000: one-minute cycle

--------------------------------------------------------------------------------------------------------------- --------

1. Experiments with Timer.scheduleatfixedrate (task, firsttime, period)

Timer.scheduleatfixedrate (New Myhouronlinetask (Event.getservletcontext ()), nexthour,1*60*1000);

Experimental results:

Hypothetical condition: The time after project start OK is 2015-01-08 10:00:00!

First: 2015-01-08 10:00:20 when the scheduled task executes once, and then starts from 2015-01-08 10:00:20, every 1 minutes.

Second: 2015-01-08 10:00:00 the scheduled task executes once, and then starts from 2015-01-08 10:00:40, and executes every 1 minutes.

Third: 2015-01-08 10:00:00 when the scheduled task executes once, every 1 minutes.

Remark Timer.scheduleatfixedrate (New Myhouronlinetask (Event.getservletcontext ()), 0,1*60*1000); 2015-01-08 10:00:00 timed tasks are executed once, every 1 minutes.

2. Experiments with Timer.schedule (task, firsttime, period)

Experimental results:

Hypothetical condition: The time after project start OK is 2015-01-08 10:00:00!

First: 2015-01-08 10:00:20 when the scheduled task executes once, and then starts from 2015-01-08 10:00:20, every 1 minutes.

Second: 2015-01-08 10:00:00 the scheduled task executes once, every 1 minutes.

Third: 2015-01-08 10:00:00 when the scheduled task executes once, every 1 minutes.

Note Timer.schedule (New Myhouronlinetask (Event.getservletcontext ()), 0,1*60*1000); when, 2015-01-08 10:00:00 the scheduled task executes once, Executes every 1 minutes.

Experiments of Timer.scheduleatfixedrate and Timer.schedule

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.