Java comes with the Java.util.Timer class, by dispatching a java.util.TimerTask task. This approach allows the program to execute at a certain frequency, but cannot specify a time to run. Use less.
The invocation of a task is performed by a child thread.
Java.util.Timer
Java.util.TimerTask
First, call the method:
(1) Void Java.util.Timer.schedule (timertask task, long delay): how long (milliseconds) to perform a task
(2) void Java.util.Timer.schedule (timertask task, Date time): Set a period to perform a task
(3) void Java.util.Timer.schedule (timertask task, long delay, long period): After the delay time, the task is started and the task is called every period time.
(4) Void Java.util.Timer.schedule (timertask task, Date Firsttime, long period): The first time a task is executed at a specified firsttime point The task is invoked once every period time.
(5) Void Java.util.Timer.scheduleAtFixedRate (timertask task, long delay, long period): Start the task after the delay time, and call the task once every period time.
(6) Void Java.util.Timer.scheduleAtFixedRate (timertask task, long delay, long period): The first time the task is executed at the specified Firsttime point, The task is invoked once every period time.
void Java.util.Timer.cancel (): Terminates the Timer
Boolean java.util.TimerTask.cancel (): Terminates the TimerTask
Multiple timertask can be specified for each timer
Although the Java.util.Timer.schedule (timertask task, Date Firsttime, long period) method can be used to complete the business of "for example: one 10 o'clock in the morning per day", However, the implementation is based on a mechanism that performs a delay of one day (1000 * 60 * 60 * 24 milliseconds) and does not specify a specific time to execute.
For this kind of demand, it can be realized by quartz.
The difference between the method name schedule () and Scheduleatfixedrate ()
When the task needs to be called multiple times according to the period interval time, there are two different strategies, two of which provide different strategies.
The calling method (1), (2) is only a single execution, there is no multiple invocation of the task, so there is no way to provide the Scheduleatfixedrate method.
<1>schedule () method more focused on maintaining the stability of the interval time: guaranteed to be callable every period time
The <2>scheduleatfixedrate () method is more focused on maintaining the stability of the execution frequency: The frequency of multiple calls is approaching period time, and if one call time is greater than period, the next one will be less than period, To guarantee frequency close to period
Iii. examples
(1)
/** * First method: Set how long (in milliseconds) to perform a task*/ Public Static voidtimer1 () {Final StopWatch watch=NewStopWatch (); Watch.start (); Timer Timer=NewTimer (); /*void Java.util.Timer.schedule (timertask task, long delay)*/Timer.schedule (NewTimerTask () { Public voidrun () {watch.stop (); System. out. println ("-------Task Execution--------"); System. out. println (Watch.gettime ()); } }, -);//delay=2000 milliseconds after the task is executed}
(2)
/** * Second method: Set a time to perform a task */public static void Timer2 () { Calendar calendar = new GregorianCalendar (); Calendar.add (Calendar.minute, 1); Calendar.set (Calendar.second, 0); A minute later executes the timer timer = new timer (); /* void Java.util.Timer.schedule (timertask task, Date time) * /Timer.schedule (new TimerTask () { @Override Public void Run () { System.out.println ("-------Task Execution--------") ,} }, Calendar.gettime ()); }
(3)
/** * Third method: Sets the execution of the specified task tasks to be fixed delay after the specified delay peroid * /public static void Timer3 () { Timer timer = new Timer (); Final StopWatch watch = new StopWatch (); Watch.start (); System.out.println (Thread.CurrentThread (). GetName ()); /* void Java.util.Timer.schedule (timertask task, long delay, long * period) * /Timer.schedule (new TimerTask () {public void run () {/ * Child thread performs the execution of the task */ System.out.println (Thread.CurrentThread (). GetName ( )); System.out.println ("-------set to specify task--------"); Watch.suspend (); System.out.println (Watch.gettime ()); Watch.reset (); Watch.start ();}} , +, +); }
(4)
/** * Fixed delay peroid time after execution; peroid time is not the start of the calculation at the time the task was executed (after a task execution completes, it is called again after peroid time). * Instead of calling the task once every peroid time. When the task executes less than peroid time, it can be guaranteed to be called every peroid time. * When the execution time of a task is greater than peroid time, from the phenomenon: once the task is executed, it will immediately go to the next task's execution */public static void Timer3a () { Timer timer = new Timer (); Final StopWatch watch = new StopWatch (); Watch.start (); Timer.schedule (New TimerTask () { Integer i = 1; public void Run () { System.out.println (Watch.gettime ()); System.out.println ("####### #第" + i + "execution start ########"); try { thread.sleep; } catch (Interruptedexception e) { e.printstacktrace (); } System.out.println ("####### #第" + i + "execution end ########"); i++; Watch.reset (); Watch.start ();}}, (+ ); }
period:500 milliseconds, 400 milliseconds per task execution time, so the time to start the next task call is nearly 100 milliseconds after each task execution completes
Print log information:
- 1000
- ####### #第1次执行开始 ########
- ####### #第1次执行结束 ########
- 109
- ####### #第2次执行开始 ########
- ####### #第2次执行结束 ########
- 93
- ####### #第3次执行开始 ########
- ####### #第3次执行结束 ########
- 93
- ####### #第4次执行开始 ########
- ####### #第4次执行结束 ########
- 93
- ####### #第5次执行开始 ########
- ####### #第5次执行结束 ########
- 93
- ####### #第6次执行开始 ########
- ####### #第6次执行结束 ########
- 93
- ####### #第7次执行开始 ########
- ####### #第7次执行结束 ########
- 93
- ####### #第8次执行开始 ########
When Thread.Sleep (400) is modified to Thread.Sleep (600):
Print log information:
- 1000
- ####### #第1 execution start ########
- ####### #第1 execution End ########
- 0
- ####### #第2 execution start ########
- ####### #第2 execution End ########
- 0
- ####### #第3 execution start ########
- ####### #第3 execution End ########
- 0
- ####### #第4 execution start ########
- ####### #第4 execution End ########
- 0
- ####### #第5 execution start ########
(5)
/** Fourth method: Schedule the specified task tasks to start at a specified time firsttime at a fixed rate of repetition period execution * Every day 12 o'clock noon*/ Public Static voidtimer4 () {Calendar Calendar=calendar.getinstance (); Calendar.Set(Calendar.hour_of_day, A);//Control TimeCalendar.Set(Calendar.minute,0);//Control PointsCalendar.Set(Calendar.second,0);//Control secondsDate Time= Calendar.gettime ();//The time to execute the task, here is today's 12:00:00Timer Timer=NewTimer (); /*void Java.util.Timer.schedule (timertask task, Date Firsttime, long period)*/Timer.schedule (NewTimerTask () { Public voidrun () {System. out. println ("-------Set the task to be specified--------"); }}, Time, +* -* -* -);//This setting will delay the execution of the daily fixed}
(6)
/** * Fifth method: Sets the execution of a fixed frequency peroid for the specified task tasks after the specified delayed delay. * Timer.schedule and Timer.scheduleatfixedrate differences: * (1) The schedule () method is more focused on maintaining the stability of the interval time: guaranteed to be callable every period time * (2) Scheduleatfixedrate () method more attention to maintain the stability of the frequency of execution: to ensure that the frequency of multiple calls toward the period time, if a call time is greater than period, next time will be less than period, to ensure that the frequency close to period */ Public Static voidTimer5 () {Timer timer=NewTimer (); /*void Java.util.Timer.scheduleAtFixedRate (timertask task, long delay, long period)*/Timer.scheduleatfixedrate (NewTimerTask () { Public voidrun () {System. out. println ("-------Set the task to be specified--------"); } }, +, -); }
Ext.: http://blog.csdn.net/eg366/article/details/11836017
Java Timer Usage Summary