Timer has two task execution modes, schedule, which is the most commonly used mode. It can execute tasks in two ways: 1: at a certain time (Data), 2: after a fixed time (int delay ). both methods can specify the frequency of task execution. let's look at a simple example:
Copy codeThe Code is as follows:
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 ("________");
}
}
}
If you are using JDK 5 + and there is a scheduleAtFixedRate mode available, in this mode, Timer tries its best to run the task at a fixed frequency. For example: in the above example, we want MyTask to be executed once every two seconds after one second, but because java is not real-time (in fact, java is not real-time .....), therefore, the original meaning we expressed in the previous program cannot be strictly enforced. if scheduleAtFixedRate is called, Timer tries its best to keep your Task execution frequency at 2 seconds. run the above program. If scheduleAtFixedRate is used, the following scenario is possible: After 1 second, MyTask is executed once because the system is busy, after 2.5 seconds, MyTask was executed for the second time. Then, Timer noted down the delay and tried to make up for it in the next task. Then, after 1.5 seconds, three times that MyTask will be executed. "execute a task at a fixed frequency rather than a fixed delay time"
The following example shows how to exit a single TimerTask and how to exit all tasks.
Copy codeThe Code is as follows:
Package MyTimerTest;
Import java. io. IOException;
Import java. util. Timer;
/*
* This class provides the main methods for using Timer and TimerTaske, including customizing tasks and adding tasks.
* Exit the task and exit the timer.
* Because the status field of TimerTask is package-level accessible, there is no way out of the java. util. Package.
* Get the status, which causes some inconvenience to programming. We cannot determine the status of a Task.
*
*/
Public class TimerTest {
Public static void main (String [] args ){
Timer timer = new Timer ();
MyTask myTask1 = new MyTask ();
MyTask myTask2 = new MyTask ();
MyTask2.setInfo ("myTask-2 ");
Timer. schedule (myTask1, 1000,200 0 );
Timer. scheduleAtFixedRate (myTask2, 2000,300 0 );
While (true ){
Try {
Byte [] info = new byte [1024];
Int len = System. in. read (info );
String strInfo = new String (info, 0, len, "GBK"); // read information from the console
If (strInfo. charAt (strInfo. length ()-1) = ''){
StrInfo = strInfo. substring (0, strInfo. length ()-2 );
}
If (strInfo. startsWith ("Cancel-1") {myTask1.cancel (); // exit a single task
// In fact, you should judge whether myTask2 has exited. If yes, you should break it. But it cannot be obtained outside the package.
// MyTask2 status. Therefore, you cannot determine whether to exit the loop.
} Else if (strInfo. startsWith ("Cancel-2 ")){
MyTask2.cancel ();
} Else if (strInfo. startsWith ("Cancel-All ")){
Timer. cancel (); // exit Timer
Break;
} Else {
// Only judge myTask1 and steal it.
MyTask1.setInfo (strInfo );
}
} Catch (IOException e) {// TODO Auto-generated catch block
E. printStackTrace ();
}
}
}
Static class MyTask extends java. util. TimerTask {
String info = "^_^ ";
@ Override
Public void run (){
// TODO Auto-generated method stub
System. out. println (info );
}
Public String getInfo (){
Return info;
}
Public void setInfo (String info ){
This.info = info;
}
}
}