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