package Test;
import java.util.Timer;
import java.util.TimerTask;
public class Test {
static boolean isEnd = false; //控制TimerTask的結束標識
static int count = 1; //循環計數器
static Timer timer = new Timer();
public static void main(String[] args) {
timer.schedule(new TimerTask(){
public void run(){
//延時或循環執行的內容
if(isEnd == true) {
System.out.println("查詢結束");
timer.cancel();
}else {
System.out.println("當前第"+count+"次查詢");
count++;
if( count == 5 ) {
isEnd = true;
}
}
}
}, 1000,1000);
//schedule{TimerTask()方法調用,延時執行時間(第一次開始執行時間),循環執行時間間隔(以後每次執行間隔時間)}
}
}