zhongyijie
发贴: 36
积分: 30
|
于 2003-07-24 10:30
import java.util.Timer; import java.util.TimerTask;
public class Reminder { Timer timer;
public Reminder(int seconds) { timer = new Timer(); timer.scheduleAtFixedRate(new RemindTask(), seconds*1000**60*60); }
class RemindTask extends TimerTask { public void run() { System.out.println("Time's up!"); //timer.cancel(); //Terminate the timer thread } }
public static void main(String args[]) { System.out.println("About to schedule task."); new Reminder(1); System.out.println("Task scheduled."); } }
1。用Calendar干什么?人家是要定时执行一项任务。 2。如果要求执行的间隔固定,应该用public void schedule(TimerTask task,Date firstTime,long period) 否则用public void scheduleAtFixedRate(TimerTask task,Date firstTime,long period),这样不管任务执行的多长时间, 到点了任务就会触发。 3.请参考Java Tutorial和JDK文档。上面有更详细的答案。
|