A timer has two modes of performing a task, the most commonly used is schedule, which can perform tasks in two ways: 1: At a certain time (Data), 2: After a certain time (int delay). Either way, you can specify how often the task executes. See a simple example:
Copy Code code 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, 2000)//after 1 seconds to perform this task, each interval of 2 seconds, if passed a data parameter, you can perform the task at a certain time.
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) {
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+, there is a scheduleatfixedrate mode to use, in this mode, the timer will try to let the task run at a fixed frequency, for example: In the above example, we want to let MyTask in 1 seconds, every two seconds to perform a , but because Java is not real time (in fact, Java real time is very bad ...), therefore, the original meaning we expressed in the previous program can not be strictly enforced. If we call Scheduleatfixedrate, then, The timer will try to keep your task running at 2 seconds. Run the above program, assuming the use of scheduleatfixedrate, then the following scenario is possible: 1 seconds later, mytask execution, because the system is busy, After the next 2.5 seconds MyTask was able to perform the second time, and then the timer noted the delay and tried to make up for the delay on the next task, then 1.5 seconds later, MyTask will execute three times. To perform a task at a fixed frequency rather than a fixed delay time.
Here's an example of a complex point that tells you how to exit a single timertask and how to quit all tasks
Copy Code code as follows:
package mytimertest;
Import java.io.IOException;
Import Java.util.Timer;
/*
* This class gives the main methods of using timer and Timertaske, including custom tasks, adding tasks
* Exit the task and exit the timer.
* Because the status domain of the TimerTask is package-level accessible, there is no way out of the Java.util package.
* Get its state, which causes some inconvenience to programming. We can't judge the state 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, 2000);
Timer.scheduleatfixedrate (MyTask2, 2000, 3000);
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 console
if (Strinfo.charat (Strinfo.length ()-1) = = ") {
Strinfo = strinfo.substring (0, Strinfo.length ()-2);
}
if (Strinfo.startswith ("Cancel-1")) {Mytask1.cancel ()//Exit individual task
In fact, it should be here to determine whether MyTask2 also quit, yes, it should break. But because it's not available outside the bag,
MyTask2 state, so there is no decision to exit the loop.
else if (Strinfo.startswith ("Cancel-2")) {
Mytask2.cancel ();
else if (Strinfo.startswith ("Cancel-all")) {
Timer.cancel ()//exit Timer
Break
} else {
Just Judge MyTask1, steal a lazy ^_^
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;
}
}
}