In Java, the Timer class is mainly usedScheduled and periodic tasks
There are two methods in this class that are hard to understand, that is, the schedule and scheduleatfixedrate methods. Here we will analyze them with examples.
(1) schedule method: "fixed-delay"; if the first execution time is delayed, the subsequent execution timePress
Photo
Last time
Actual execution completion time
Computing
(2) scheduleatfixedrate: "fixed-rate"; if the first execution time is delayed, the subsequent execution timeFollow
Last time
Time Point
For the sake of "catch up", the task will be executed multiple times.Synchronization considerations
Simpledateformat dateformatter = new simpledateformat ("yyyy/mm/dd hh: mm: SS"); <br/> date startdate = dateformatter. parse ("2010/11/26 00:20:00"); <br/> timer = new timer (); <br/> timer. scheduleatfixedrate (New timertask () {<br/> Public void run () <br/>{< br/> system. out. println ("execute task! "+ This. scheduledexecutiontime (); <br/>}< br/>}, startdate, 3x60*1000 );
The preceding Code indicates that execution starts at 00:20:00 on and is executed every 3 minutes.
Assume that the task is executed at 00:27:00.
The above will be printed three times
Execute task! 00: 20
Execute task! 00:23 catch up
Execute task! 00:26 catch up
The next execution time is, relative
If you change to the schedule method, run the command at 00:27:00 on.
Will print once
Execute task! No catch up
The next execution time is, relative
All of the above considerations are that the program is executed only after the specified timer start time.
What happens when the task execution time exceeds the cycle interval?
(1) schedule method: The next execution time is relative
Last time
Actual execution completion time
Therefore, the execution time will be delayed.
(2) scheduleatfixedrate method: The next execution time is relativeLast time
Time Point
So the execution time will not be delayed,Concurrency
In the following example, timertask is executed for 6 seconds, but the interval is 5 seconds.
Package test; <br/> Import Java. text. parseexception; <br/> Import Java. text. simpledateformat; <br/> Import Java. util. date; <br/> Import Java. util. timer; <br/> Import Java. util. timertask; <br/> public class test {</P> <p> Public static void main (string [] ARGs) throws parseexception {<br/> simpledateformat dateformatter = new simpledateformat ("yyyy/mm/dd hh: mm: SS"); <br/> date startdate = dateformatter. parse ( "2010/11/28 01:06:00"); <br/> timer = new timer (); <br/> timer. schedule (New timertask () {<br/> Public void run () {<br/> try {<br/> thread. sleep (6000); <br/>} catch (interruptedexception e) {<br/> E. printstacktrace (); <br/>}< br/> system. out. println ("execute task! "+ This. scheduledexecutiontime (); <br/>}< br/>}, startdate, 5*1000); <br/>}</P> <p>}
The execution result of the schedule method is as follows:
Execute task! 1290877560001
Execute task! 1290877566001
Execute task! 1290877572001
Execute task! 1290877578001
Execute task! 1290877584001
Execute task! 1290877590001
Execute task! 1290877596001
Execute task! 1290877602001
Execute task! 1290877608001
Execute task! 1290877614001
Execute task! 1290877620001
Execute task! 1290877626001
Execute task! 1290877632001
Execute task! 1290877638001
We can see that the interval is 6 seconds. Therefore,Next execution time point = time point of the last execution completion + interval
The execution result of the scheduleatfixedrate method is as follows:
Execute task! 1290877860000
Execute task! 1290877865000
Execute task! 1290877870000
Execute task! 1290877875000
Execute task! 1290877880000
Execute task! 1290877885000
Execute task! 1290877890000
Execute task! 1290877895000
Execute task! 1290877900000
Execute task! 1290877905000
Execute task! 1290877910000
Execute task! 1290877915000
Execute task! 1290877920000
Execute task! 1290877925000
Execute task! 1290877930000
We can see that the interval is 5 seconds. Therefore,Next execution time point = last execution time point + interval
And because the previous task needs to be executed for 6 seconds and the current task has started to be executed, the two tasks overlap,Thread Synchronization considerations