The timer class timer is in the Java. util package. In use, the instance is first instantiated, and then the schedule (timertask task, long delay) method of the instance is used to set the specified task to be executed after the specified delay. The timer task class timertask is an abstract class that inherits and overwrites its run () method to implement specific tasks.
Schedule (timertask task, date time) sets the task to be executed at the specified time.
The cancel () method ends this timer.
The schedule (timertask task, long delay, long period) method is used to set the scheduled task to execute a fixed-latency peroid after the specified delay.
The scheduleatfixedrate (timertask task, long delay, long period) method sets a fixed frequency of peroid execution for a specified task after a specified delay of delay.
To implement a scheduled task, using the timer and timertask classes in Java can easily call processing functions in real time. These two classes are very convenient to use and can fulfill the vast majority of timer needs.
Let's look at a simple example:
Import java. Io. ioexception;
Import java. util. timer;
Public class timertest {
Public static void main (string [] ARGs ){
Timer timer = new timer ();
Timer. schedule (New mytask (), 1000,200 0); // execute this task after 1 second. Each interval is 2 seconds. If you pass a data parameter, you can execute this task at a fixed time.
While (true) {// This is used to stop this task, otherwise it will be executed cyclically.
Try {
Int CH = system. In. Read ();
If (CH-'c' = 0 ){
Timer. Cancel (); // use this method to exit the task
}
} Catch (ioexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
}
}
Static class mytask extends java. util. timertask {
@ Override
Public void run (){
// Todo auto-generated method stub
System. Out. println ("________");
}
}
}
=========== Java timer application in the Web ==============
In the web, the timer is generally started with the Web server. There are two methods.
Method 1: configure a servlet in Web. xml and set it to start with Web server startup. Start the timer in the init () method of the servlet and destroy the timer in the destory () method.
Method 2: configure a listener in Web. XML, start the timer in the listener initialization method, and destroy the timer from the left to the left.
Start the timer in Servlet
Java code
Import java. Io. ioexception;
Import java. util. timer;
Import javax. servlet. requestdispatcher;
Import javax. servlet. servletcontext;
Import javax. servlet. servletexception;
Import javax. servlet. http. httpservlet;
Import javax. servlet. http. httpservletrequest;
Import javax. servlet. http. httpservletresponse;
Import org. Apache. commons. Lang. stringutils;
Public class convergedataservlet extends httpservlet {
Private Static final long serialversionuid = 1l;
Private timer timer1 = NULL;
Private task task1;
/**
* Constructor of the object.
*/
Public convergedataservlet (){
Super ();
}
/**
* Destruction of the servlet.
*/
Public void destroy (){
Super. Destroy ();
If (timer1! = NULL ){
Timer1.cancel ();
}
}
Public void doget (httpservletrequest request, httpservletresponse response)
Throws servletexception, ioexception {
}
Public void dopost (httpservletrequest request, httpservletresponse response)
Throws servletexception, ioexception {
Doget (request, response );
}
// Start the timer using the init Method
Public void Init () throws servletexception {
Servletcontext context = getservletcontext ();
// (True is to refresh the cache at a specified time)
String starttask = getinitparameter ("starttask ");
// Timed refresh time (minutes)
Long Delay = long. parselong (getinitparameter ("delay "));
// Start the timer
If (starttask. Equals ("true ")){
Timer1 = new timer (true );
Task1 = new task (context );
Timer1.schedule (task1, delay x 60*1000, delay * 60*1000 );
}
}
}
Scheduled operation
Java code
/**
*
* @ Author Callan
*/
Import java. util. timertask;
Import javax. servlet. servletcontext;
Public class task extends timertask {
Private servletcontext context;
Private Static Boolean isrunning = true;
Public task (servletcontext context ){
This. Context = context;
}
@ Override
Public void run (){
If (isrunning ){
}
}
}
The servlet is called when the web. xml servlet is configured to start the service.
<Servlet> </servlet>
<Servlet>
<Servlet-Name> taskservlet </servlet-Name>
<Servlet-class> com. Task </servlet-class>
<Init-param>
<Param-Name> starttask </param-Name>
<Param-value> true </param-value>
</Init-param>
<Init-param>
<Param-Name> intervaltime </param-Name>
<Param-value> 1 </param-value>
</Init-param>
<Load-on-startup> 300 </load-on-startup>
</Servlet>
<Servlet> </servlet>