1. Concept
The timed scheduled task function mainly uses Timer object in Java, which uses multi-threading internally for processing, so it is still very related to multi-threading technology. In JDK, the Timer class is mainly responsible for the function of planning tasks, that is, starting to execute a certain task at a specified time, but the class that encapsulates the task is the TimerTask class.
Customize the task to be executed by inheriting the TimerTask class and implementing the run() method:
public class Mytask extends TimerTask { @Override public void run() { DateFormat dateFormat = TimeUtil.df.get(); System.out.println("My task is running" + dateFormat.format(new Date())); }}Run the task at the execution time by executing Timer.schedule(TimerTask task,Date time):
public class Run { private static Timer timer=new Timer(); public static void main(String[] args) throws ParseException { timer.schedule(new Mytask(), TimeUtil.df.get().parse("2017-09-14 09:19:30")); }}Note: Time conversion tool class ensures thread safety:
public class TimeUtil{ public static final ThreadLocal<DateFormat> df = new ThreadLocal<DateFormat>() { @Override protected DateFormat initialValue() { return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); } };} 2. Timer class precautions
1. Creating a Timer object means a new thread is started, but the newly started thread is not a daemon thread. It has been running in the background. The newly started Timer thread can be set as a daemon thread through the following.
private static Timer timer=new Timer(true);
2. In advance: When the planned time is earlier than the current time, the task will be run immediately.
3. Delay: TimerTask is run one by one in a queue, so the execution time may be inconsistent with the time you expect, because the previous task may take a long time, and 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 operation: Timer.schedule(TimerTask task,Date firstTime, long period) Starting from firstTime, tasks are executed every milliseconds of period:
5. schedule(TimerTask task, long delay) The current time is the reference time. After delaying the set number of milliseconds on this time, the TimerTask task is executed.
6. schedule(TimerTask task, long delay, long period) The current time is the reference time. On this basis, delay the set number of milliseconds, and then execute a task with an infinite number of times at a certain interval.
7. What is the difference between Timer's cancel() and TimerTask's cancel()?
The execution of tasks mentioned earlier is executed one by one in the form of columns. TimerTask.cancel() refers to canceling the current task from the task-to-column. The value of Timer.cancel() is to cancel all tasks in the current task queue. It is worth noting that Timer's cancel() sometimes does not necessarily stop executing planned tasks, but executes them 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 be executed normally.
3. The difference between scheduleAtFixedRate (TimerTask task,Date firstTime,long period) and schedule(TimerTask task,Date firstTime,long period)
Similarities:
1. The method schedule and the method scheduleAtFixedRate are executed in order, so non-thread safety situations are not required.
2. Method schedule and method scheduleAtFixedRate If the time of executing a task is not delayed, then the execution time of the next task is calculated based on the time at the "start" of the previous task.
3. Method schedule and method scheduleAtFixedRate If the time for executing a task is delayed, the execution time of the next task is calculated by referring to the time at the end of the last task.
Differences:
There is basically no difference between the method schedule and the method scheduleAtFixedRate in use, that is, scheduleAtFixedRate has catch-up execution. What does it mean? That is, if the task is interrupted during periodic operation, scheduleAtFixedRate will try to fill in the previously dropped tasks. And the schedule is ignored, and then run the next task. You can refer to this blog, which is written vividly.
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.