1, Timer timer basic usage
- Timer timer = new Timer(); timer.schedule(TimerTask, Date);, The task needs to be encapsulated with TimerTask, and the run method in TimerTask is rewritten
- Timer timer = new Timer(boolean);, Passing a value of true indicates that the timer thread is a daemon thread, and the end of the main thread follows the end. The default value is false. The main thread ends and the task is executed, but the thread has not ended.
- If the current time is after Date, it will be executed immediately; if it is before Date, it will be executed after the specified time
- Different scheduled tasks called by the same Timer are executed in sequence
- The Timer may delay the execution. If the same Timer is used to call the schedule() method, the first thread will take a long time to execute. When the second thread has passed the preset time, it will be executed immediately.
import java.util.*; class MyTask extends TimerTask { @Override public void run() { System.out.println("MyTask Run time:" + new Date()); try { Thread.sleep(20000); } catch (InterruptedException e) { e.printStackTrace(); } } } public class StudyThreads01Timer usage { public static void main(String[] args) { System.out.println("Current time:" + new Date()); // Set the running time. If the running time is before the current time, it will be executed immediately, otherwise it will be executed after the specified time Calendar calendar = new GregorianCalendar(); calendar.add(Calendar.SECOND, 5); Date runDate = calendar.getTime(); MyTask myTask = new MyTask(); calendar.add(Calendar.SECOND,10); Date runDate2 = calendar.getTime(); MyTask myTask2 = new MyTask(); System.out.println("Task 1 scheduled time:"+runDate); System.out.println("Task 2 scheduled time:"+runDate2); // Create a Timer object. The value Boolean can be passed to indicate whether it is a daemon thread (the end of the main thread follows the end) // If the main thread is not a daemon thread, the scheduled task thread will not end. When it is a daemon thread, the scheduled task thread will end whether it is executed or not Timer timer = new Timer(); timer.schedule(myTask, runDate); Timer timer2 = new Timer(); timer2.schedule(myTask2 , runDate2); } }
2, Schedule (TimerTask, date, firsttime, long period) method
- The period passed in indicates that it is executed every other period of time
- date is executed after the current time according to the specified time, and immediately before the current time
- The task execution time is too long, resulting in the delay of subsequent tasks. The subsequent tasks are executed according to the queue. For example, they are executed every 2s regularly, but the task execution needs 5s, then they are executed every 5s in the future.
- TimerTask.cancel() method to clear itself from the task queue, and other tasks will not be affected.
- Timer.cancel() method to empty all task queues in timer. Note that it may not be cleared because the Timer.cancel() method may not grab the lock of the task queue
Schedule (TimerTask, long delay)
- Execute after delay milliseconds based on the current time
- schedule(TimerTask task, long delay, long period) is executed based on the current time, after a delay of milliseconds, once per period
schedule(TimerTask task, long delay, long period) vs. schedule (TimerTask task, date, long period) vs. scheduleatfixedrate (TimerTask task, date, long period) vs. scheduleatfixedrate (TimerTask task, date, long period)
- If the task does not delay, when Date is passed in, the start time of last task execution + period is the start time of this task. When long is passed in, the start time of the last task + period is the start time of the current task. scheduleAtFixedRate is similar
- If the task is delayed, when Date is passed in, the last task end time + period is the start time of the current task. When long is passed in, the last task end time + period is the start time of the current task. scheduleAtFixedRate is similar
What is the difference between scheduleAtFixedRate and schedule
- schedule is not catch-up: when a task is delayed for a long time, the task that has not been executed in the middle task will be cancelled and will not be executed.
- scheduleAtFixedRate is catch-up. When the task delay time is very long, the intermediate task will perform supplementary line execution.
scheduleAtFixedRate test code:
package com.chapter05; import java.util.*; class MyTask02 extends TimerTask { @Override public void run() { System.out.println("MyTask Run time:" + new Date()); } } public class StudyThreads02scheduleAtFixedRate method { public static void main(String[] args) { Calendar calendar = Calendar.getInstance(); calendar.add(Calendar.SECOND, -20); Date runDate = calendar.getTime(); System.out.println("Current time:" + new Date()); System.out.println("Planned execution time:" + runDate); MyTask02 myTask02 = new MyTask02(); Timer timer = new Timer(); timer.scheduleAtFixedRate(myTask02, runDate, 2000); } }
Operation results: