📄 timertrial.java
字号:
package basic.timer;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
public class TimerTrial {
private static Timer _timer = new Timer();
private static FirstTask _firstTask = new FirstTask();
private static SecondTask _secondTask = new SecondTask();
private static ThirdTask _thirdTask = new ThirdTask();
private static FourthTask _fourthTask = new FourthTask();
private static long _delay = 0;
private static long _period = 0;
private static Date _executeTime = null;
public TimerTrial(long delay, long period) {
_delay = delay * 1000;
_period = period * 1000;
_executeTime = new Date();
}
public static void main(String[] args) {
TimerTrial timerTrial = new TimerTrial(3, 2);// 3 means three
// milliseconds
timerTrial.startUp();
// timerTrial.shutDown();
}
private void startUp() {
_timer.schedule(_firstTask, _delay);// 指定经过一个时间段(Here is 3
// seconds)才做的事情。
_timer.schedule(_secondTask, _executeTime);// 在指定的时间点做一件事情,如果被制定的时间已经是当前系统的过去时间,则立刻执行run()方法。
_timer.schedule(_thirdTask, _delay, _period);// 从指定经过一个时间段(_delay)后,每隔一段时间(_period)重复做一件事情。
_timer.schedule(_fourthTask, _executeTime, _period);// 在指定的时间点做一件事情,然后每隔一段时间(_period)重复的做同一件事情
}
private void shutDown() {
_timer.cancel();// 关掉定时器
}
}
class FirstTask extends TimerTask {
public void run() {
System.err.println("First.Schedules the specified task for execution after the specified delay.");
System.err.println("");
}
}
class SecondTask extends TimerTask {
public void run() {
System.err.println("Second.Schedules the specified task for execution at the specified time.");
System.err.println("");
}
}
class ThirdTask extends TimerTask {
public void run() {
System.out
.println("Third.Schedules the specified task for repeated fixed-delay execution, beginning after the specified delay.");
System.err.println("");
}
}
class FourthTask extends TimerTask {
public void run() {
System.err
.println("Fourth.Schedules the specified task for repeated fixed-delay execution, beginning at the specified time.");
System.err.println("");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -