Java timed Dispatch __java

Source: Internet
Author: User

Scheduled scheduling: Whenever a period of time, the program will be automatically executed, called scheduled scheduling.

If you want to use scheduled scheduling, you must ensure that the program is always running before you can, that is, the equivalent of scheduled scheduling is to start a new thread outside the program.

Timer and TimerTask two timer schedules.





If you want to schedule a timer class now, you need to use the TimerTask subclass.

According to some concepts, the completion of a scheduled scheduling program, every 2 seconds to print a time.

Import Java.util.TimerTask;
Import Java.util.Date;
Import Java.text.SimpleDateFormat;
Class MyTask extends timertask{//task scheduling classes inherit TimerTask
public void Run () {
SimpleDateFormat SDF = null;
SDF = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss.") SSS ");
SYSTEM.OUT.PRINTLN ("Current system time is:" + Sdf.format (new Date ());
}
};

Complete is a specific task operation class, the later scheduled scheduling is this kind of operation, the Main method is run () method.

Set up test classes and perform task scheduling.

Import Java.util.Timer;
public class testtask{
public static void Main (String args[]) {
Timer T = new timer (); Creating a Timer Class object
MyTask MyTask = new MyTask (); Defining tasks
T.schedule (mytask,1000,2000); Set task execution, start in 1 seconds, repeat every 2 seconds
}
}; Summarize:

This content is generally useful in the development of the web, because maintaining a container without shutting down can be done on a regular basis.

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.