Differences between schedule and scheduleAtFixedRate of timer in java: timer. schedule

Source: Internet
Author: User

Differences between schedule and scheduleAtFixedRate of timer in java: timer. schedule

The schedule and scheduleAtFixedRate methods of timer are basically the same. They only differ when a specific situation occurs-the current task has not been completed before the next task is handed in.

 

Here is an example:

During the summer vacation, the teacher assigned assignments to schedule and scheduleAtFixedRate.

The teacher asked the students to write 2 pages a day during the summer vacation and finish their homework 30 days later.

The two students completed their homework on time every day until the first day. An accident occurred. It took five days for the two students to go out for a tour. During these five days, neither of them did their homework. The task is delayed.

At this time, the two students adopt different strategies:

Schedule rescheduled the task time. It took 35 days to complete the task on the first day of the tour.

ScheduleAtFixedRate is a punctual student. She always wants to finish the teacher's task on time. So on the first day of the tour, she completed all the tasks she had owed for the last five days and those on the seventh day, it took 30 days to complete the assignment according to the teacher's original arrangement.

 

1 package day01; 2 3 import java. text. simpleDateFormat; 4 import java. util. timer; 5 import java. util. timerTask; 6 7 public class Test01 {8 public static void main (String [] args) {9 final Timer timer = new Timer (); 10 // timer. scheduleAtFixedRate (new TimerTask () {11 timer. schedule (new TimerTask () {// comment out this line and the above line respectively to try 12 int count = 1; 13 14 @ Override15 public void run () {16 count ++; 17 if (count = 10) {18 try {19 Thread. sleep (5000); 20} catch (InterruptedException e) {21 System. out. println ("latency 5 s"); 22 e. printStackTrace (); 23} 24} 25 SimpleDateFormat sf = new SimpleDateFormat (26 "yyyy MM dd hh: mm: ss"); 27 System. out. println ("current time:" 28 + sf. format (System. currentTimeMillis () + "scheduled time:" 29 + sf. format (scheduledExecutionTime (); 30} 31 }, 1000,100 0); 32} 33}

Hope to help you!

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.