Java Task Scheduler

Source: Internet
Author: User
Tags string format

1.Timer

 packagecom.qhong;Importjava.text.SimpleDateFormat;Importjava.util.Date;Importjava.util.Timer;Importjava.util.TimerTask; public classMain { public Static voidmain (string[] Args) {timer timer=NewTimer (); LongDelay1 = 1 * 1000; LongPERIOD1 = 1000; //1 seconds from now, once every 1 seconds job1Timer.schedule (NewTimertest ("job1" +dateutils.format (NewDate (), "yyyy-mm-dd HH:mm:ss") ), delay1, period1); LongDelay2 = 2 * 1000; LongPERIOD2 = 2000; //2 seconds from now, once every 2 seconds job2Timer.schedule (NewTimertest ("job2"), delay2, period2); }}classTimertestextendsTimerTask {PrivateString JobName = "";  publictimertest (String JobName) {Super();  this. JobName =jobName; } @Override public voidrun () {System.out.println ("execute" +jobName); }}classDateutils {/**Time Format (yyyy-mm-dd)*/     public Final StaticString Date_pattern = "yyyy-mm-dd"; /**time format (yyyy-mm-dd HH:mm:ss)*/     public Final StaticString Date_time_pattern = "yyyy-mm-dd HH:mm:ss";  public StaticString format (date Date) {returnformat (date, date_pattern); }     public Staticstring Format (date date, string Pattern) {if(date! =NULL) {simpledateformat DF=NewSimpleDateFormat (pattern); returnDf.format (date); }        return NULL; }}
Execute job12017-03-10 19:48:45execute job2execute job12017-03-10 19:48:45execute job12017-03-10 19:48:45Execute job2execute job12017-03-10 19:48:45execute job12017-03-10 19:48:45Execute Job2execute job12017-03-10 19:48:45execute job12017-03-10 19:48:45execute Job2execute job12017 -03-10 19:48:45execute job12017-03-10 19:48:45

The advantage of a Timer is that it is easy to use, but because all tasks are scheduled by the same thread, all tasks are executed serially, only one task is executed at a time, and the delay or exception of the previous task affects the subsequent Task.

Scheduledexecutor

 packagecom.qhong;Importjava.util.Date;Importjava.text.SimpleDateFormat;Importjava.util.concurrent.Executors;Importjava.util.concurrent.ScheduledExecutorService;Importjava.util.concurrent.TimeUnit; public classMain { public Static voidmain (string[] Args) {scheduledexecutorservice service= Executors.newscheduledthreadpool (10); LongInitialDelay1 = 1; LongPERIOD1 = 1; //1 seconds from now, once every 1 seconds job1Service.scheduleatfixedrate (NewScheduledexecutortest ("job1"), initialDelay1, period1, timeunit.seconds); LongInitialDelay2 = 2; LongDelay2 = 2; //2 seconds from now, once every 2 seconds job2Service.schedulewithfixeddelay (NewScheduledexecutortest ("job2"), initialDelay2, delay2, timeunit.seconds); }}classScheduledexecutortestImplementsRunnable {PrivateString JobName = "";  publicscheduledexecutortest (String JobName) {Super();  this. JobName =jobName; } @Override public voidrun () {System.out.println ("execute" + Dateutils.format (NewDate (), "yyyy-mm-dd HH:mm:ss") +jobName); }}classDateutils {/**Time Format (yyyy-mm-dd)*/     public Final StaticString Date_pattern = "yyyy-mm-dd"; /**time format (yyyy-mm-dd HH:mm:ss)*/     public Final StaticString Date_time_pattern = "yyyy-mm-dd HH:mm:ss";  public StaticString format (date Date) {returnformat (date, date_pattern); }     public Staticstring Format (date date, string Pattern) {if(date! =NULL) {simpledateformat DF=NewSimpleDateFormat (pattern); returnDf.format (date); }        return NULL; }}
 Execute 2017-03-10 20:03:53job1execute  2017-03-10 20:03:54job2execute  2017-03-10 20:03:54job1execute  2017-03-10 20:03:55job1execute  2017-03-10 20:03:56job1execute  2017-03-10 20:03:56job2execute  2017-03-10 20:03:57job1execute  2017-03-10 20:03:58job1execute  2017-03-10 20:03:58job2execute  2017-03-10 20:03:59job1execute  2017-03-10 20:04:00job1execute  2017-03-10 20:04:0 0job2execute  2017-03-10 20:04:01job1execute  2017-03-10 20:04:0 2job1execute  2017-03-10 20:04:02job2 

Two of the most commonly used scheduling methods, scheduleatfixedrate and schedulewithfixeddelay, are shown in Scheduledexecutorservice.

Scheduleatfixedrate Each execution time is pushed back one time interval for the last task, that is, each execution time is: initialdelay, initialdelay+period, initialdelay+2*period, ... ;

Schedulewithfixeddelay each execution time pushes back one time interval for the last task, that is, each execution time is: initialdelay, initialdelay+executetime+delay, Initialdelay+2*executetime+2*delay.

therefore, Scheduleatfixedrate is based on fixed time interval for task scheduling, Schedulewithfixeddelay depends on the duration of each task execution, is based on the time interval of the task Scheduling.

https://www.ibm.com/developerworks/cn/java/j-lo-taskschedule/

Java Task Scheduler

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.