In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the relevant knowledge of "how to use Java Timer and TimerTask classes to make program timing execution". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "how to use Java Timer and TimerTask classes to make program timing execution" can help you solve the problem.
Timer&TimerTask class
Realize the automatic execution of program tasks according to the set time schedule
Each scheduling task class needs to inherit the java.util.TimerTask parent class, and the task needs to be started through the java.util.Timer class.
The TimerTask class common method functions public void cancel () terminates the task the specific operation that the task needs to perform public long scheduleExecutionTime () returns the last time the task is to be executed the Timer class common method functions public Timer () to create a timer and start the timer public void cancel () to terminate the timer and abandon all scheduled tasks There is no effect on the currently executing task public int purge () removes all cancelled tasks to free memory space public void schedule (TimerTask task,Date time) arranges a task to be executed at a specified time, and if that time has been exceeded, execute public void schedule (TimerTask task,Date firstTime,long period) immediately to schedule a task to execute at the specified time Then repeat public void schedule at a fixed frequency (TimerTask task,long delay) schedule a task to be executed after a period of time public void schedule (TimerTask task,long delay,long period) schedule a task to be executed after a period of time, and then repeat public void schedule AtFixedRate at a fixed frequency (TimerTask task,Date firstTime,long period) schedule a task to be executed at a specified time Then repeat public void schedule AtFixedRate with approximately fixed frequency (TimerTask task,long delay,long period) schedule a task to be executed after a period of time, and then repeat at an approximately fixed frequency
The execution interval of the schedule () method is always fixed, and if there is a delay before, it will continue to execute at the set interval.
The scheduleAtFixedRate () method automatically adjusts the execution time of the next interval according to the delay time that occurs.
Demo:
Import java.util.Timer;import java.util.TimerTask;class Test extends TimerTask {@ Override public void run () {System.out.println (Thread.currentThread (). GetName () + "time:" + System.currentTimeMillis ());}} public class Hello {public static void main (String [] args) {Timer timer = new Timer (); timer.scheduleAtFixedRate (new Test (), 100Power1000);}}
Timer.scheduleAtFixedRate (new Test (), 100pl 1000)
Define the interval task, which starts in 100 milliseconds and executes 1 second every 1 second
This is the end of the introduction on "how to use Java Timer and TimerTask classes to make program timing execution". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
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.