Detailed explanation of Java timer usage
1、 Concept
The timer object is mainly used by the scheduled task planning function in Java. It uses multithreading internally, so it is very related to multithreading technology. In JDK, timer class is mainly responsible for the function of planning tasks, that is, starting to execute a task at a specified time, but the class encapsulating the task is TimerTask class.
Customize the task to be executed by inheriting the TimerTask class and implementing the run () method:
By executing timer Schedule (TimerTask task, date time) runs the task at execution time:
Note: time conversion tool class to ensure thread safety:
2、 Precautions for timer class
1. To create a timer object is to start a new thread, but the newly started thread is not a daemon thread. It always runs in the background. You can set the newly started timer thread as a daemon thread through the following steps.
2. Advance: when the planned time is earlier than the current time, the task will be run immediately.
3. Delay: TimerTask runs one by one in a queue, so the execution time may be inconsistent with your expected time. Because the previous task may take a long time, the running time of the subsequent task will be delayed. The specific start time of the delayed task is based on the "end time" of the previous task
4. Periodic run: timer Schedule (TimerTask, task, date, firsttime, long period) executes the task every millisecond from firsttime:
5. The current time of schedule (TimerTask task, long delay) is the reference time. Based on this time, the TimerTask task is executed once after delaying the specified number of milliseconds.
6. Schedule (TimerTask task, long delay, long period) the current time is the reference time. On this basis, delay the specified number of milliseconds, and then execute a task indefinitely at a certain interval.
7. What is the difference between timer's cancel() and TimerTask's cancel()?
As mentioned earlier, tasks are executed one by one in sequence in the form of columns. TimerTask Cancel () refers to canceling the current task from the task pair column. Timer. The value of cancel () cancels all tasks in the current task queue. It is worth noting that timer's cancel() sometimes does not necessarily stop executing the planned task, but executes normally. This is because the cancel () method in the timer class sometimes does not compete for the queue lock, so the tasks in the TimerTask class continue to execute normally.
3、 What is the difference between scheduleatfixedrate (TimerTask task, long period) and schedule (TimerTask task, long period)
Similarities:
1. Both the method schedule and the method scheduleatfixedrate are executed sequentially, so non thread safety is not considered.
2. Method schedule and method scheduleatfixedrate if the execution time of the task is not delayed, the execution time of the next task is calculated by referring to the "start" time of the previous task.
3. Method schedule and method scheduleatfixedrate if the execution time of the task is delayed, the execution time of the next task is calculated by referring to the time at the end of the previous task.
difference:
There is basically no difference in the use of method schedule and method scheduleatfixedrate, that is, scheduleatfixedrate has catch-up execution. What does it mean? If a task is interrupted during periodic running, scheduleatfixedrate will try to make up the previously dropped task for running. The schedule doesn't matter, and then run the next task. You can refer to this blog, which is very vivid.