Java Timer analysis

Source: Internet
Author: User

Some time ago to do a SPRINGMVC project, there is a function is to pull the data on a timed basis. The practice is to start a timer, timed this line. Java has its own timers, but is not well integrated in Springmvc, and because of the use of SPRINGMVC, it is

Most of them are annotations, which are more efficient to write. Of course the quart timer is very useful and powerful. To be lazy, find spring comes with a lightweight timer spring schedule, which can be implemented using a single line of annotations. The code is this:

Add a line <task:annotation-driven/> to the spring annotations, represent the annotations that support this timer, and then add a line @scheduled (cron = "0 55 23 * *") to the method on the Timer class, Of course this class must be added

Into the spring component. It's easy, a timer is implemented, and it's easy to modify it, so change the cron expression.

Because of the increase of the function, there are several places where the timer is needed, and I do not think about it, the original method is copied over, and I will not think of any problems. When the project test was released that day, it suddenly found that the content of the run was not, and the key was that the log

No error. This problem does not know the reason, is not good to solve. Can only go back to the project, one by one to hit the log, Debug. Later found that the timer is not all started, started a. Later found if just a timer, run no problem, is this

Only one timer is supported? I am not very likely to, write a code test, found that several are no problem. It's possible that the timer is blocked at the same time and can only perform a single task, otherwise, several tasks in the project need to be kept running.

, which means taking a few threads. began to want to see the source of sprng schedule, feeling there is too much involved in the class. First read the Java comes with the source of the timer, found that two in the processing of the problem is very similar to see a bit

Java.util.timer, its core code of execution, is actually a thread that always polls the individual timers, since it is a thread, then it is definitely blocked. Let's take a brief look at the analysis below.

There are three classes in the core, Timer,timetask, including two important class taskqueue,timerthread in the Timer. The class diagram is as follows (only the body function is described)

  

  

First of all, timertask,virgin,scheduled,executed,cancelled represents the status of execution: Not executed, executed, executed, canceled.

method, run means that the task executes, cancel, cancels the task, and Scheduledexecutiontime returns the next time the task is executed.

The timer is actually a façade interface that provides the access interface for timed tasks schedule

Taskqueue is actually a timertask container, and the property contains an TimerTask array.

The method is the management of the task

Add to add a new scheduled task

Getmin, get the time of the most recent scheduled task

Removemin, remove the most recent scheduled task

Reschedulemin, re-scheduling time

Fixup, the timing task moves forward,

Fixdown, move the scheduled task backwards.

In general, this is both the management and scheduling of scheduled tasks.

The main realization part of timing is in the TimerThread

TimerThread implements the thread interface, which means that the timer function is implemented through this thread,

The main code in the Mainloop code is as follows

  

The function is known to scan the TimerTask task in Taskqueue, each time it takes the most recent scheduled task, if the time is not reached, then the thread waits, if the judgment turns to a timer execution, then the thread blocks until the thread finishes. Because a single thread is polled, there can be only one scheduled task executing at a time point. This way, if a task executes too long, the other tasks will have to wait, and if a task is executed all the time, then the remaining tasks will never be executed and blocked. This is similar to me in spring schedule. Perhaps the function I use is similar to the one that Java comes with, using a single-threaded blocking pattern.

There is time to analyze spring Schedule,quart

Java Timer analysis

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.