"Java" timer

Source: Internet
Author: User

Summarize

1. The task of executing the plan is placed in the subclass of TimerTask, which is performed by the timer.

2, create a timer is to start a new thread, until the timer in the completion of the task, will not end. The thread that you want to create is a daemon thread, and you need to set the timer timer=new timer (TRUE) when you create it;

3. The task will be executed immediately before the current time.

4. TimerTask is executed sequentially in a queue. When the previous task consumes a long time, the subsequent task runs at a later time.

For example, assume that the Task1 execution requires 2s,task2 to be executed after the Task1 starts 1s. The result is that Task2 is executed immediately after Task1 is executed. (actually also in line with the 3rd)

5. If there is no delay, the time to execute the task is the time of the last task start plus poried. If it is deferred, the time to execute the task is the end time of the previous task. (Supplementary article 4th)

6, the difference between the method schedule and Scheduleatfixedrate is that the latter has the pursuit of execution. (explained by code below)

7, abnormal aspects. If an exception occurs while executing a task in the timer, the timer thread is ended and no tasks are performed.

Common examples

1. Example

ImportJava.util.Calendar;Importjava.util.Date;ImportJava.util.Timer;ImportJava.util.TimerTask;/*** Created by Zay on 2016/12/8.*/ Public classTimertest { Public Static voidMain (string[] args) {System.out.println ("Current time:" +NewDate ()); //The task that executes the plan is placed in a subclass of TimerTask, which is performed by the timer. Timer timer =NewTimer (); //Create a task that starts after 1s and executes every 2s thereafter. Timer.schedule (NewTimerTask () {@Override Public voidrun () {System.out.println (NewDate () + "task One"); }        },1000,2000); //Create a task that executes after 10 days. Calendar Calendar =calendar.getinstance (); Calendar.add (Calendar.date,1); Date rundate=Calendar.gettime (); Timer.schedule (NewTimerTask () {@Override Public voidrun () {System.out.println (NewDate () + "task Two");                }},rundate); //Timer.cancel ();        Calling the timer's cancel () method empties all tasks for that timer. //Timertask.cancel (); Calling TimerTask's Cancel () method cancels the task in the timer.     }}

2, the difference between the method schedule and Scheduleatfixedrate is that the latter has the pursuit of execution.

ImportJava.util.Calendar;Importjava.util.Date;ImportJava.util.Timer;ImportJava.util.TimerTask;/*** Created by Zay on 2016/12/8.*/ Public classTimertest { Public Static voidMain (string[] args) {System.out.println ("Current time:" +NewDate ()); Calendar Calendar=calendar.getinstance (); Calendar.add (Calendar.second,-100);//set a relatively early timeDate earlydate =Calendar.gettime (); Timer Timer=NewTimer (); System.out.printf (NewDate () + "Start task Now"); Timer.scheduleatfixedrate (NewTimerTask () {@Override Public voidrun () {System.out.println (NewDate () + "task One"); }},earlydate,5000); /*Timer.schedule (New TimerTask () {@Override public void run () {SYSTEM.OUT.PRINTLN            (New Date () + "task One"); }},earlydate,5000);*/    }}

Output of the Schedule method (note the time to perform the task) the output of the//scheduleatfixedrate method

Explanation: When the task is executed earlier than the current time, such as in the example, the time to perform the task is earlier than the current task 100s, then this time theory should be some of the tasks how to deal with it?

Schedule choose to abandon, taking the current time as a new starting point, and scheduleatfiexdrate will take a breath of these tasks are completed.

"Java" timer

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.