In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
What this article shares with you is about what Timer is. The editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article. Without saying much, follow the editor to have a look.
The Timer class is a utility class that Java provides threads for timing task calls.
The Timer class is thread safe.
The Timer class uses binary heap data structures to store tasks.
Timer class constructor
Timer (): create a new timer
Timer (boolean isDaemon): creates a new timer that specifies its associated thread to run as a daemon
Timer (String name): creates a new timer whose associated thread has the specified name
Timer (String name,boolean isDaemon): creates a new timer whose associated thread has the specified name and can be specified to run as a daemon
Methods of the Timer class
Cancel (): terminates this timer and discards any currently scheduled tasks. Does not interfere with tasks that are currently in progress, if any. Once the timer terminates, its thread of execution terminates normally and tasks can no longer be scheduled on it.
Purge (): removes all cancelled tasks from the task queue of this timer.
Schedule (TimerTask task, Date time): a specified task scheduled to be performed at a specified time. Where task indicates the task to be scheduled and time indicates the time it takes to execute the task.
Schedule (TimerTask task, Date firstTime, long period): schedule specified tasks for repeated fixed delays, starting at a specified time. Where the time when firstTime executes the task for the first time, and period represents the time between successive task execution (in milliseconds)
Schedule (TimerTask task, long delay): schedule the execution of the specified task after the specified delay syntax. Where delay represents the delay (in milliseconds) before the task is executed.
Schedule (TimerTask task, long delay, long period): starting after the specified delay syntax, scheduling specified tasks for repeated fixed delays.
ScheduleAtFixedRate (TimerTask task,Date firstTime,long period): starting at a specified time, schedule a specified task for repetitive, fixed-rate execution.
ScheduleAtFixedRate (TimerTask task, long delay, long period): starting after the specified delay syntax, scheduling the specified task for repeated fixed rate execution.
The difference between schedule () and scheduleAtFixedRate ()
Identical point
1. Both method schedule and method scheduleAtFixedRate are executed sequentially, so there is no need to consider non-thread-safe situations.
2. Method schedule and method scheduleAtFixedRate if the execution time of the task is not delayed, then the execution time of the next task is calculated with reference to the "start" time of the previous task.
3. Method schedule and method scheduleAtFixedRate if the execution time of the task is delayed, then the execution time of the next task is calculated with reference to the "end" time of the last task.
Differences
There is basically no difference between schedule and scheduleAtFixedRate in use. ScheduleAtFixedRate is catch-up and executive. if a task is interrupted during a periodic run, scheduleAtFixedRate will try to make up for the task that fell behind before. Schedule, on the other hand, just leave it and run the next task.
How to understand the cases of schedule and scheduleAtFixedRate
During the summer vacation, the teacher assigned homework to schedule and scheduleAtFixedRate. The teacher asked the students to write 2 pages every day during the summer vacation and finish the homework 30 days later. The two students finished their homework on time every day until the 10th day. In an accident, it took the two students five days to travel, and neither of them did their homework in the five days. The mission was delayed. At this point, the two students adopted different strategies:
Schedule rescheduled the task, doing the 11th day of the task on the first day of the trip, and the 12th day of the second day, and it took 35 days to complete the task.
ScheduleAtFixedRate is a punctual student. She always wants to finish her teacher's tasks on time, so on the first day of her trip, she finished all the tasks she owed in the previous 5 days and the tasks on the 16th day. After that, she finished her homework according to the teacher's original arrangement, and it took 30 days to finish the task.
This is what Timer is, and the editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.