Timer class timer in the Java.util package. When used, first instantiate, then use the instance's schedule (timertask task, long delay) method to set the specified task tasks to execute after the specified delay delay. The timer task Class TimerTask is an abstract class that inherits and overrides its run () method to achieve specific tasks.
Schedule (timertask task, Date time) specifies that task tasks are executed at a specified time.
The Cancel () method ends this timer.
Schedule (timertask task, long delay, long period) method setting specifies that task tasks perform a fixed delay delay after specifying a delay peroid.
Scheduleatfixedrate (timertask task, long delay, long period) method setting specifies that task tasks perform a fixed frequency delay after specifying a delay peroid.
To implement a timed task, the timer and TimerTask classes in Java can be very easy to implement real-time call processing functions. These two classes are easy to use and can accomplish most of our needs for timers.
Look at a simple example: according to the time set by the configuration file doxxx
The configuration file is as follows: Time.properties
#delay执行任务前的延迟时间
#internal执行各后续任务之间的时间间隔
Delay=1
Internal=3
The code is as follows:
/** * Timer * @author Zhoucan * * * * * * */public class Time {Private timer timer, public time () {timer = new timer ();} private T Imertask task = new TimerTask () {public void run () {//doxxx System.out.println (' Do Me ');}; /** * Start Timer */public void start () {InputStream is = This.getclass (). getResourceAsStream ("/time.properties"); Properties Pro = new properties (); try {pro.load (is);} catch (IOException e) {e.printstacktrace (); SYSTEM.OUT.PRINTLN ("Read configuration file failed!"); int delay = Integer.parseint (Pro.getproperty ("delay")); int internal = Integer.parseint (Pro.getproperty ("internal")); Timer.schedule (task, delay*1000,internal*1000); public static void Main (string[] args {Time time = new Time (); Time.start ();}}