Timing scheduling for Java

Source: Internet
Author: User

generally in web development Timing scheduling is useful, because to maintain a container does not close to be able to continue to operate on a regular basis.

Scheduled scheduling: Whenever a period of time, the program will automatically execute, called scheduled scheduling. If you want to use timed transfers, you must ensure that the program is always running, which means that the scheduled schedule is a new thread that starts outside of the program.

completion of scheduled scheduling needs support for Timer and timertask Classes

Timer class:
    • The Timer class is a threading facility that can be used to schedule a task to execute once, or periodically, after a certain time or time. Give the function to be used in conjunction with the timertask class. the timertask class is used to implement A task that is scheduled for one or more executions by the Timer class.
    • each timer object corresponds to a thread, so the tasks performed by the timer should be completed quickly, or the subsequent tasks will be deferred, and the subsequent tasks may be stacked together until the task is completed quickly and quickly.

   

the difference between the schedule () and the scheduleatfixedrate () methods:

The difference is in the handling of delays in time intervals when the task is repeated:

    • The execution interval of the L schedule () method is fixed, and if there is a delay, then it will continue to execute at the specified time interval;
    • The L scheduleatfixedrate () method can automatically adjust the execution time of the next interval based on the delay time that occurs.

TimerTask class:

To perform a specific task, you must use the TimerTask class. the timertask class is an abstract class that, if you want to use this class, requires you to create a class that inherits this class and implements the abstract method in it.

   

follow this concept to complete a time scheduler that prints time every 2 seconds.

define a class foundation TimerTask class, complete the specific task.

1 import Java.text.SimpleDateFormat;2 import java.util.Date;3 import Java.util.TimerTask;4 5  Public classMyTask extends timertask{//task scheduling classes inherit this class6 @Override7      Public voidRun () {//complete a specific task8SimpleDateFormat SDF =NewSimpleDateFormat ("YYYY-MM-DD HH:mm:ss:SSS");9System. out. println ("Current system time:"+sdf.format (NewDate ()));Ten     }     One}

The completion is a specific task operation class, and later scheduled to tune is the Run () method in this class .

1  Public classTesttimertask {//Test Class2      Public Static voidMain (String args[]) {3Timer T =NewTimer ();//set up a timer object4MyTask MT =NewMyTask ();//Defining Tasks5T.schedule (MT, 1000,2000);//set the execution of the task, starting in 1 seconds and executing once every 2 seconds6     }7}

Timing scheduling for Java

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.