Timer Task 的简单范例(相当触发器,转自Sun)
类别: JAVA教程
Scheduling a Timer Task to Run at a Certain Time
==============================================
Date timeToRun =
new Date(System.currentTimeMillis()+
numberOfMillisecondsInTheFuture);
Timer timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
// Task here
}
}, timeToRun);
============================================
Scheduling a Timer Task to Run Repeatedly
=============================================
// delay for 5 sec.
int delay = 5000;
// repeat every sec.
int period = 1000;
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
// Task here
}
}, delay, period);
==============================================
Date timeToRun =
new Date(System.currentTimeMillis()+
numberOfMillisecondsInTheFuture);
Timer timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
// Task here
}
}, timeToRun);
============================================
Scheduling a Timer Task to Run Repeatedly
=============================================
// delay for 5 sec.
int delay = 5000;
// repeat every sec.
int period = 1000;
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
// Task here
}
}, delay, period);
-= 资 源 教 程 =-
文 章 搜 索