在你的程序一启动的时候就启动Timer罗
Timer t = new java.util.Timer();
t.scheduleAtFixedRate(task, firstTime, period);
然后你写一个继承TimerTask的类,在run方法中来给出你运行的工作
Timer t = new java.util.Timer();
t.scheduleAtFixedRate(task, firstTime, period);
然后你写一个继承TimerTask的类,在run方法中来给出你运行的工作
{
Timer timer;
public ReminderTest()
{
timer = new Timer();
timer.schedule(new RemindTask(),3*1000,3*1000);
} 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.");
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 21);
calendar.set(Calendar.MINUTE, 35);
calendar.set(Calendar.SECOND, 30);
Date timeRun = calendar.getTime();
System.out.println("Task scheduled.");
new ReminderTest();
System.out.println("over!");
}
}
TimerTask是从哪儿来的?
偶看不明白?
why?
:)
Timer需要的参数是它Timer有两种schedule方式,我一般都是用timer.scheduleAtFixRate
这样才是每??时间运行一次,
schedule那个方法是说上个任务的结束时间到下次任务开始时间之间保持固定