Use of the Java timer timer

Source: Internet
Author: User

I. Delay execution first, we define a class, give it a name called Timetask, and our timed task is executed in the main function of this class.

The code is as follows:
Package test;
Import Java.util.Timer;
public class Timetasktest {
public static void Main (string[] args) {

Timer timer = new timer ();
Timer.schedule (New Task (), 60 * 1000);
}
}
Explain the code above. The above code implements a function that executes a task after a minute after the Timetasktest program is started. Very simple: First new a Timer object, and then call its schedule method, this method has four overloaded methods, here we use one,
public void Schedule (TimerTask task,long delay)


First, the first parameter of the first parameter is the task we are going to perform. This is an TimerTask object, which is exactly the object of a class that implements TimerTask, because TimerTask is an abstract class. In the above code, the task is our own definition of the implementation of the TimerTask class, because it is in the same package, so there is no explicit import come in.

The code for the task class is as follows
Package test;
Import Java.util.TimerTask;
public class Task extends TimerTask {

public void Run ()
{
SYSTEM.OUT.PRINTLN ("Timed Task Execution");
}

}
Our task must implement the TimerTask method run, and the task to be performed is within the Run method, where we only let it line the console.

The second argument is a long value. This is the delay time, that is, how much time to perform scheduled tasks after the start of the program. The value of this long is the number of milliseconds, so in the previous program, the parameter value that was executed after one minute is 60 * 1000.

Second, the loop to set the timing of the task, often we need to perform such tasks repeatedly, every time, and the above method is executed only once, so that the schedule method is another overloaded function

public void Schedule (TimerTask task,long Delay,long period)
The first two parameters do not have to say anything, the last parameter is the interval of time, but also a long type of milliseconds. For example, we want the above task to be executed once every minute after the first execution, and the third parameter value will be 60 * 1000 OK.

Third, the designated execution time since it is known as a scheduled task, we certainly hope that we can specify the time specified by the task, obviously the above method is not used, because we do not know when the program began to run, there is no way to determine how much delay. It doesn't matter, schedule four overloaded method is not finished yet. Use the following to be OK:
public void Schedule (TimerTask task,date time)
For example, we want the timed task to be executed 0:0 July 2, 2006, as long as the second parameter is passed a time set to the Date object on July 2, 2006 0:0. In one case, it may be July 3, 2006 when our program is started, so that when the program starts, the scheduled task starts to execute. Schedule the last overloaded method is public void schedule (TimerTask task,date Firsttime,long period)

Four, the timing task in the Java EE in the actual project, often scheduled tasks need to work on the resources of the Web project, in this way, with the above single program may be a little too much, because a lot of web engineering resources it can not operate. The workaround is to use the servlet to put the code that executes the timed task into the servlet's init () function.

Source: http://zhidao.baidu.com/question/83065736

Use of the Java timer timer

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.