Principle and implementation of Java Multithread timer

preface

The timing / scheduling function is widely used in various fields of Java applications. For example, at the web level, a project may collect bills regularly, update some caches regularly, clean up a group of inactive users regularly, etc. The timer object is mainly used by the scheduled task planning function in Java. It uses multithreading internally, so it is quite related to multithreading technology. Like ThreadLocal, it's better to talk about the principle before using it. The implementation principle of timer is not difficult. Just sweep it.

Use of timer's schedule (TimeTask task, date time)

The function of this method is to execute the task once on the execution date

1. The time when the task is executed is later than the current time: it will be executed in the future

Take a look at the operation effect:

The execution time is inconsistent with the previous time, but it is consistent with the dateref time, which proves the future execution. Although the task has been executed, the process has not been destroyed. The box on the console is still red. Look at the source code of timer:

Therefore, starting a timer is to start a new thread, but the new thread is not a daemon thread, so it will run all the time. To stop the process after running, just set timer as the daemon thread. There is a special constructor to set:

2. The scheduled time is earlier than the current time: execute now

If the task is executed earlier than the current time, execute the task immediately:

Take a look at the operation effect:

The execution time is consistent with the current time, which proves that it is executed immediately

3. Multiple TimerTask task execution

Multiple tasks are allowed in timer:

Take a look at the running results:

String time: 2015-10-612:26:00 current time: 2015-10-612:25:38

String time: 2015-10-612:27:00 current time: 2015-10-612:25:38

It's running! Time: tueoct0612:26:00cst2015

It's running! Time: tueoct0612:27:00cst2015

It can be seen that the running time is consistent with the set time, which proves that multiple tasks can be performed in the future. In addition, note that tasks are executed one by one in a queue, so the execution time may be inconsistent with the expected time, because the previous tasks may consume too much and the running time of subsequent tasks may be delayed.

The code will not be written. For example, task 1 plans to be executed at 12:00:00, and task 2 plans to be executed at 12:00:10. As a result, task 1 has been executed for 30 seconds, so task 2 will be executed at 12:00:30. Because tasks are put into the queue, they must be run one by one.

Timer's schedule (timertasktask, datefirsttime, longperiod)

The function of this method is to execute a person periodically and indefinitely at a specified interval after the specified date

1. The planned time is later than the current time: it will be executed in the future

Take a look at the running results:

String time: 2015-10-6 18:01:00 current time: 2015-10-6 18:00:15 run! Time: Tue OCT 06 18:01:00 CST 2015 is running! Time: Tue OCT 06 18:01:04 CST 2015 running! Time: Tue OCT 06 18:01:08 CST 2015 running! Time: Tue OCT 06 18:01:12 CST 2015

From the set time, print every 4 seconds and print indefinitely

2. The scheduled time is earlier than the current time: execute now

Take a look at the running results:

String time: 2014-10-6 18:01:00 current time: 2015-10-6 18:02:46 run! Time: Tue OCT 06 18:02:46 CST 2015 running! Time: Tue OCT 06 18:02:50 CST 2015 running! Time: Tue OCT 06 18:02:54 CST 2015 running! Time: Tue OCT 06 18:02:58 CST 2015 running! Time: Tue OCT 06 18:03:02 CST 2015

See that the running time is earlier than the current time. Start from the current time, print every 4 seconds, and cycle indefinitely

Cancel() method of TimerTask

The cancel() method of TimerTask is used to clear itself from the task queue:

Take a look at the running results:

See that the cancel () method of TimeTask is to remove itself from the task queue, and other tasks are not affected

Cancel() method of timer

Change the above code:

Take a look at the running results:

String time: 2015-10-618:10:00 current time: 2015-10-618:14:15

A is running! Time: tueoct0618:14:15cst2015

All tasks are cleared and the process is destroyed. Note, however, that the cancel () method may not necessarily stop executing the planned task, but may execute normally, because the cancel () method will try to obtain the queue lock. If the queue lock is not obtained, it is entirely possible for the task in the TimerTask class to continue executing

Other methods

To enumerate the functions of other schedule overloaded methods in timer, there is no code to prove. You can try it yourself:

1、schedule(TimerTasktask,longdelay)

The TimerTask task is executed once after the specified number of milliseconds is delayed based on the current time

2、schedule(TimerTasktask,longdelay,longperiod)

Taking the current time as the reference, after delaying the specified number of milliseconds based on this time, the TimerTask task is executed circularly with period as the cycle cycle cycle

3、scheduleAtFixedRate(TimerTasktask,longperiod)

In the delay scenario, there is no difference between the schedule method and the scheduleatfixedrate method. The difference is only in non delay. If the task execution time is not delayed, for the schedule method, the next task execution time is calculated by referring to the start time of the previous task; For the scheduleatfixedrate method, the execution time of the next task is calculated by referring to the end time of the previous task

summary

The above is all about the principle and implementation of Java multithreaded timer in this paper. I hope it will be helpful to you. Interested friends can continue to refer to this website:

Java multithreaded programming example

On the advantages of Java multithreading and code examples

Implementation code of readwritelock read-write separation of Java multithreading

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>