Multi-thread learning-Timer, multi-thread-timer
You can use Timer to execute scheduled tasks. Now let's take a look at a demo to summarize the learned tasks.
1 public class TimerTest {2 3 public static void main (String [] args) throws ParseException {4 Date date = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss "). parse ("11:18:00"); 5 new Timer (). schedule (new TimerTask () {6 @ Override 7 public void run () {8 System. out. println (new Date (). getSeconds () + "execution"); 9} 10}, date); 11} 12}
This Code indicates that a Timer is created first, and the start time of Timer execution is "11:18:00 ".
Repeated Timer execution
The above code Timer will only be executed once. What if we want the scheduled task to be executed at intervals? In this case, another overload of the schedule method provided by Timer can be used as public void schedule (TimerTask task, long delay, long period ). It indicates that Timer starts to execute the task during delay and runs the task once every period.
1 public class TimerTest {2 3 public static void main (String [] args) throws ParseException {4 Date date = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss "). parse ("11:18:00"); 5 new Timer (). schedule (new TimerTask () {6 @ Override 7 public void run () {8 System. out. println (new Date (). getSeconds () + "execution"); 9} 10}, date, 1000); 11} 12}
Execution result
37 execution
38 execution
39 execution
40 execution
......
Timer execution latency
When TimerTask is executed, the thread execution time may be too long, exceeding the Timer wait time. What will happen now?
1 public class TimerTest {2 3 public static void main (String [] args) throws ParseException {4 Date date = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss "). parse ("11:18:00"); 5 new Timer (). schedule (new TimerTask () {6 @ Override 7 public void run () {8 System. out. println (new Date (). getSeconds () + "start execution"); 9 try {10 Thread. sleep (2000); 11 System. out. println (new Date (). getSeconds () + "execution ended"); 12} catch (InterruptedException e) {13 e. printStackTrace (); 14} 15} 16}, date, 1000); 17} 18}
View execution results
0 start execution
2 execution ended
2. Start execution.
4. Execution ended
4. Start execution.
6 execution ended
6. Start execution.
8 execution ended
8. Start execution.
ScheduleAtFixedRate
Use the schedule method to execute the Timer task. If the start time is before the current time, the Timer does not perform the tasks that were not previously executed. That is, after the delay, the Timer starts from the current time and then runs according to the interval. However, sometimes we need to pay more attention to the frequency of execution, and we need to supplement the tasks that are not executed due to delay. scheduleAtFiexdRate is used at this time.
1 public class TimerTest {2 3 public static void main (String [] args) throws ParseException {4 final Date date = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss "). parse ("11:52:00"); 5 new Timer (). scheduleAtFixedRate (new TimerTask () {6 @ Override 7 public void run () {8 System. out. println (date); 9 System. out. println (new Date (). getSeconds () + "start execution"); 10 System. out. println ("executed"); 11 System. out. println (new Date (). getSeconds () + "execution ended"); 12 13} 14}, date, 10000); 15} 16}
Execution result
39 start execution
Executed
39 execution ended
Thu Aug 17 11:52:00 CST 2017
39 start execution
Executed
39 execution ended
Thu Aug 17 11:52:00 CST 2017
40 execution started
Executed
40 execution ended
It can be seen from the execution results that the scheduleAtFixedRate first fills in the missing execution, and then starts to execute the execution once according to the interval.
Set Timer as a daemon thread
1 public class TimerTest {2 3 public static void main (String [] args) throws ParseException {4 final Date date = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss "). parse ("11:52:00"); 5 Timer timer = new Timer (); 6 TimerTask timerTask = new TimerTask () {7 @ Override 8 public void run () {9 System. out. println (date); 10 System. out. println (new Date (). getSeconds () + "start execution"); 11 System. out. println ("executed"); 12 System. out. println (new Date (). getSeconds () + "execution ended"); 13 14} 15}; 16 timer. schedule (timerTask, date); 17} 18}
After the code is executed, the program is still running. Check Timer. the source code of the class can be found that Timer creates a new thread, while the continuous tracking code finds that the new thread carries out a while (true) loop, and then enters the waiting state.
1 public Timer(String name) {2 thread.setName(name);3 thread.start();4 }
Solution
Set Timer to the daemon thread. Timer has a constructor.
1 public Timer(boolean isDaemon) {2 this("Timer-" + serialNumber(), isDaemon);3 }
So we can change new Timer () to new Timer (true.
The cancel Method of TimerTask and Timer
The cancel Method of TimerTask is to close this task, and the cancel Method of Timer is to close the entire Timer.
Timer actually starts a thread, maintains a queue in the thread, and puts TimerTask into the queue. The cancel Method of TimerTask is to remove itself from the task queue. The Timer cancel Method clears all tasks in the queue.