In general, there is no difference between timer's schedule and scheduleAtFixedRate methods, only when a certain situation occurs, there will be a difference - the current task has not had time to complete the next task and hand it over again.
Let's give an example:
During the summer vacation, the teacher assigned homework to schedule and scheduleAtFixedRate.
The teacher asked students to write 2 pages a day during the summer vacation and complete their homework after 30 days.
The two students completed their homework on time every day until the 10th day. When something happened, it took them 5 days for the two students to go out for a trip. During these 5 days, neither of them did their homework. The mission was delayed.
At this time, the strategies adopted by the two students were different:
schedule rescheduled the task time. On the first day of the trip, I did the 11th day of the mission and the 12th day of the second day. It took 35 days to complete the task.
scheduleAtFixedRate is a punctual student. She always wants to complete the teacher's tasks on time. So on the first day of her trip, she completed all the tasks owed by the previous five days and the tasks on the 16th day. After that, she still completed the homework according to the teacher's original arrangement, and finally completed the task 30 days.
package day01;import java.text.SimpleDateFormat;import java.util.Timer;import java.util.TimerTask;public class Test01 { public static void main(String[] args) { final Timer timer = new Timer(); //timer.scheduleAtFixedRate(new TimerTask() { timer.schedule(new TimerTask() {//Annotate this line and the above line to try the effect int count = 1; @Override public void run() { count++; if (count == 10) { try { Thread.sleep(5000); } catch (InterruptedException e) { System.out.println("Delay 5s"); e.printStackTrace(); } } SimpleDateFormat sf = new SimpleDateFormat( "yyyy MM dd hh:mm:ss"); System.out.println("Current time:" + sf.format(System.currentTimeMillis()) + "Scheduled time:" + sf.format(scheduledExecutionTime())); } }, 1000, 1000); }}Hope it can help everyone!
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.