Jdk timer and jdk Timer

Source: Internet
Author: User

Jdk timer and jdk Timer

First, let's take a look at the built-in jdk Timer:

A tool used by a thread to schedule tasks to be executed in a background thread. You can schedule the task to be executed once, or perform the task repeatedly on a regular basis. And eachTimerObjects correspond to a single background thread used to sequentially execute all timer tasks. The timer task should be completed quickly. If it takes too long to complete a timer task, it will "exclusively" the task execution thread of the timer. Therefore, this may delay the execution of subsequent tasks, and these tasks may be "heap together" and can be quickly and continuously executed only when the unfriendly tasks are finally completed.

Schedule(TimerTask task, long delay) scheduled to execute the specified task after the specified delay.
Schedule(TimerTask task, Date time) scheduled to execute the specified task at the specified time. If the time has passed, the task is scheduled to be executed immediately.
Schedule(TimerTask task, long delay, long period) Schedule the specified task to repeat from the specified delayFixed latency execution. If an execution is delayed for any reason (such as garbage collection or other background activities), subsequent execution will also be delayed
Schedule(TimerTask task, Date firstTime, long period) Schedule the specified task to repeat at the specified timeFixed latency execution. If an execution is delayed for any reason (such as garbage collection or other background activities), subsequent execution will also be delayed.

1 package test; 2 3 import java. text. parseException; 4 import java. text. simpleDateFormat; 5 import java. util. date; 6 import java. util. timer; 7 import java. util. timerTask; 8 9/** 10 * jdk built-in timer 11*12 * @ author LIUTIE13 * 14 */15 public class JDKTimer {16 17 18 public static void main (String [] args) throws ParseException {19 // Date Format tool 20 final SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss "); 21 22 Timer timer = new Timer (); 23 // after 10 s, run the Timer, only once 24 System. out. print (sdf. format (new Date (); 25 System. out. println ("the timer one will be executed after 10 seconds... "); 26 long milliseconds = 10*1000; 27 timer. schedule (new TimerTask () {28 29 @ Override30 public void run () {31 System. out. print (sdf. format (new Date (); 32 System. out. println ("the timer one has finished execution"); 33} 34}, milliseconds); the timer is executed after 35 36 // 12 seconds, and 37 System is executed every 1 s. out. print (sdf. format (new Date (); 38 System. out. println ("the timer two will be executed after 12 seconds... "); 39 // delay time after startup 40 long afterSs = 12*1000; 41 // execution cycle 42 long intervalSs1 = 1*1000; 43 timer. schedule (new TimerTask () {44 // execute counter 45 int I = 0; 46 47 @ Override48 public void run () {49 System. out. print (sdf. format (new Date (); 50 System. out. println ("the timer two has execution" + (++ I) + "timers"); 51 // close the timer after 10 times if (I = 10) {53 this. cancel (); 54} 55} 56}, afterSs, intervalSs1); 57 58 59 // The timer is executed at the specified time and only 60 systems are executed once. out. print (sdf. format (new Date (); 61 System. out. println ("the timer three will be executed at 21:47:00... "); 62 Date date = sdf. parse ("21:47:00"); 63 timer. schedule (new TimerTask () {64 65 @ Override66 public void run () {67 System. out. print (sdf. format (new Date (); 68 System. out. println ("the timer three has finished execution"); 69} 70}, date); 71 72 // periodically execute 73 System from the specified time. out. print (sdf. format (new Date (); 74 System. out. println ("the timer four will be executed at 21:48:00... "); 75 // execution interval 76 long intervalSs = 1*1000; 77 // execution start time 78 Date beginTime = sdf. parse ("21:48:00"); 79 timer. schedule (new TimerTask () {80 // execute counter 81 int I = 0; 82 83 @ Override84 public void run () {85 System. out. print (sdf. format (new Date (); 86 System. out. println ("the timer four has execution" + (++ I) + "timers"); 87 // after 10 executions, disable the timer 88 if (I = 10) {89 this. cancel (); 90} 91} 92}, beginTime, intervalSs); 93} 94 95}
View Code

Execution result

2017-06-27 21:46:24the timer one will be executed after 10 seconds...2017-06-27 21:46:24the timer two will be executed after 12 seconds...2017-06-27 21:46:24the timer three will be executed at 2017-06-27 21:47:00...2017-06-27 21:46:24the timer four will be executed at 2017-06-27 21:48:00...2017-06-27 21:46:34the timer one has finished execution2017-06-27 21:46:36the timer two has execution 1 timers2017-06-27 21:46:37the timer two has execution 2 timers2017-06-27 21:46:38the timer two has execution 3 timers2017-06-27 21:46:39the timer two has execution 4 timers2017-06-27 21:46:40the timer two has execution 5 timers2017-06-27 21:46:41the timer two has execution 6 timers2017-06-27 21:46:42the timer two has execution 7 timers2017-06-27 21:46:43the timer two has execution 8 timers2017-06-27 21:46:44the timer two has execution 9 timers2017-06-27 21:46:45the timer two has execution 10 timers2017-06-27 21:47:00the timer three has finished execution2017-06-27 21:48:00the timer four has execution 1 timers2017-06-27 21:48:01the timer four has execution 2 timers2017-06-27 21:48:02the timer four has execution 3 timers2017-06-27 21:48:03the timer four has execution 4 timers2017-06-27 21:48:04the timer four has execution 5 timers2017-06-27 21:48:05the timer four has execution 6 timers2017-06-27 21:48:06the timer four has execution 7 timers2017-06-27 21:48:07the timer four has execution 8 timers2017-06-27 21:48:08the timer four has execution 9 timers2017-06-27 21:48:09the timer four has execution 10 timers
View Code

 

Related Article

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.