A simple understanding of the difference between the schedule and Scheduleatfixedrate methods of a timer in Java

Source: Internet
Author: User

The schedule and Scheduleatfixedrate methods of the timer are generally not the same, only in a situation where there will be a difference-the current task does not have time to complete the next task and hand over.

Let's give an example:

Summer vacation to the teacher to schedule and scheduleatfixedrate two students decorate homework.

The teacher asked the students to write 2 pages a day in the summer and finish their homework after 30 days.

The two students finished their homework on time every day, until the 10th day, an accident, two students went out to travel for 5 days, this 5 days time two people did not do homework. The task was delayed.

At this time the two students adopted a different strategy:

Schedule rescheduled the task time, travel back to the first day to do the task of the 11th day, the next day to do the 12th day of the task, the final completion of the task took 35 days.

Scheduleatfixedrate is a punctual student, she always want to finish the teacher's task on time, so in the first day of travel back to 5 days before the task and the 16th day of the task completed, after the teacher's original arrangement to complete the work, the final completion of the task took 30 days.

1  Packageday01;2 3 ImportJava.text.SimpleDateFormat;4 ImportJava.util.Timer;5 ImportJava.util.TimerTask;6 7  Public classTest01 {8      Public Static voidMain (string[] args) {9         FinalTimer timer =NewTimer ();Ten         //timer.scheduleatfixedrate (New TimerTask () { OneTimer.schedule (NewTimerTask () {//comment on the line and the above line, respectively, and try the effect . A             intCount = 1; -  - @Override the              Public voidrun () { -count++; -                 if(Count = = 10) { -                     Try { +Thread.Sleep (5000); -}Catch(interruptedexception e) { +SYSTEM.OUT.PRINTLN ("Delay 5s"); A e.printstacktrace (); at                     } -                 } -SimpleDateFormat SF =NewSimpleDateFormat ( -"YYYY MM DD hh:mm:ss"); -System.out.println ("Current Time:" -+ Sf.format (System.currenttimemillis ()) + "Schedule Time:" in+Sf.format (Scheduledexecutiontime ())); -             } to}, 1000, 1000); +     } -}

Hope to help everyone!

A simple understanding of the difference between the schedule and Scheduleatfixedrate methods of a timer in Java

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.