Use of timed triggers (timer,timertask)

Source: Internet
Author: User
(API Yue) public class TimerThe extends Object thread uses it to schedule tasks to be performed later in the background thread. You can schedule tasks to execute once, or repeat them periodically. Used to perform all timer tasks sequentially. The timer task should be completed quickly. If a timer task is completed too long, it will "monopolize" the task execution thread of the timer. Thus, this may delay the execution of subsequent tasks, which may be "stacked together" and can be executed quickly and continuously when the aforementioned unfriendly task is finally completed. Simple example: Import java.io.IOException;
Import Java.util.Timer;
Import Java.util.TimerTask;

Import Java.util.Date;

public class Testtimer {
public static void Start () {
Inittimer ();
}

public static void Inittimer () {
Timer T = new timer (); Create a Timer object
TimerTask tt = new TimerTask () {
public void Run () {
System.out.println ("Now" + New Date ());
}
};//creates a TimerTask object, the Timer object invokes the TimerTask run () method

T.schedule (TT, 0, 3000);
while (true) {//This is used to stop this task, or it will loop through the task.
try {
int ch = System.in.read ();
if (CH-' c ' = = 0) {
T.cancel (); Use this method to exit a task

}
catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}

public static void Main (string[] args) {
Start ();
}
}

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.