The built-in timing tasks timertask and scheduledexecutorservice of JDK and their integration in spring

Source: Internet
Author: User

1, Java. util. timer and timertask are built-in scheduled task implementation classes in jdk1.3, which are very simple to use. However, due to system time dependency, the execution may change in the case of time jump. If the time is modified backward (in the future direction), the task execution is not affected. However, if the time is modified forward (in the past direction), the scheduled task may be delayed for a long time before it returns to normal.ProgramDuring the running process, it may not be what we expected. For some key tasks dependent on scheduled tasks, it may cause serious consequences.

 

 
/*** @ Param ARGs */public static void main (string [] ARGs) {timertask task = new timertask () {public void run () {system. out. println ("timer task execute" ++ times + "at" + new date () + ", expected executing at" + new date (this. scheduledexecutiontime (); }}; timer TM = new timer ();/*** timer is accurate based on the system time. During execution: * If the system time is modified later, the program will continue to be executed based on taskfired = (executiontime <= currenttime) in the mainloop method of the Timer class, the interval is not affected. * If the system time is modified, it is determined based on the above statement. The next execution time will be postponed and the interval will be restored to normal */TM. schedule (task, new date (), 2*60*1000 );}

2. Since jdk1.5 is started, we can select scheduledexecutorservice to replace timer for scheduled tasks. Scheduledexecutorservice is not based on absolute time and cycle, but on time delay and cycle. In this way, when a time jump occurs (Network Delay/time server synchronization/manual intervention ), the scheduled task is still executed according to the original time interval.

 
Public class jdkscheduledexecutorservice {Private Static int times = 0; Private Static long nextexecutetime = new date (). gettime ();/*** @ Param ARGs */public static void main (string [] ARGs) {timertask task = new timertask () {public void run () {nextexecutetime + = 2x60*1000; system. out. println ("timer task execute" ++ times + "at" + new date () + ", expected executing at" + new date (nextexecutetime ));}}; scheduledexecutorservice service = executors. newscheduledthreadpool (1); service. scheduleatfixedrate (task, 0, 2, timeunit. minutes );}}

The above two paragraphsCodeYou can modify the time backward during running, observe the console output effect, and then modify the time forward to observe the effect.

Spring schedule supports multiple scheduled tasks. For more information, see http://static.springsource.org/spring/docs/3.0.x/reference/scheduling.html#threadPoolTaskExecutor.

When the timer is used, the configuration is as follows (out of date, we recommend that you do not use it again)

<Context: component-scan base-package = "com. bryan. schedule "use-default-filters =" false "> <context: Include-filter type =" RegEx "expression =" com. bryan. schedule. springtimer. * "/> </Context: component-scan> <bean id =" testtask "class =" org. springframework. scheduling. timer. scheduledtimertask "> <property name =" timertask "> <ref bean =" timer "/> </property> <property name =" Period "> <value> 120000 </value> </property> <property name = "delay"> <value> 0 </value> </property> </bean> <Bean class = "org. springframework. scheduling. timer. timerfactorybean "> <property name =" scheduledtimertasks "> <list> <ref local =" testtask "/> </List> </property> </bean>

When scheduledexecutorservice is used, the configuration is as follows:

 
<Bean id = "testtask" class = "org. springframework. scheduling. concurrent. scheduledexecutortask "> <property name =" runnable "> <ref bean =" timer "/> </property> <property name =" Period "> <value> 120000 </value> </property> <property name = "delay"> <value> 0 </value> </property> <property name = "fixedrate"> <value> true </value> </property> </bean> <Bean class = "org. springframework. scheduling. concurrent. scheduledexecutorfactorybean "> <property name =" scheduledexecutortasks "> <list> <ref local =" testtask "/> </List> </property> </bean>

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.