timer問題,請教

xudaorong 2003-04-26 09:41:03
看看我的程序,為深麼他只輸出"Time's up!"一次,按道理他應該每隔1秒輸出Time's up!"才對雅,為深麼.
package testtesttest;

import java.util.Timer;
import java.util.TimerTask;

/**
* Simple demo that uses java.util.Timer to schedule a task to execute
* once 5 seconds have passed.
*/

public class Reminder {
Timer timer;

public Reminder(int seconds) {
timer = new Timer();
timer.schedule(new RemindTask(), seconds*1000);

}

class RemindTask extends TimerTask {
public void run() {
System.out.println("Time's up!");

}
}

public static void main(String args[]) {
System.out.println("About to schedule task.");
new Reminder(1);
System.out.println("Task scheduled.");
}
}
...全文
83 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
huangjiuyt 2003-04-26
  • 打赏
  • 举报
回复
zzhangwa(化石和石头)
說的對
zzhangwa 2003-04-26
  • 打赏
  • 举报
回复
public void schedule(TimerTask task,
long delay,
long period)

你用的方法中的第二个参数是延迟的时间而不是循环处理的时间,可以利用上面的方法实现。
例子如下
package testtesttest;

import java.util.Timer;
import java.util.TimerTask;

/**
* Simple demo that uses java.util.Timer to schedule a task to execute
* once 5 seconds have passed.
*/

public class Reminder {
Timer timer;

public Reminder(int seconds) {
Timer timer = new Timer();
timer.schedule(new RemindTask(), seconds*1000,1000);

}

class RemindTask extends TimerTask {
public void run() {
System.out.println("Time's up!");

}
}

public static void main(String args[]) {
System.out.println("About to schedule task.");
new Reminder(1);
System.out.println("Task scheduled.");
}
}
wyqiang 2003-04-26
  • 打赏
  • 举报
回复
schedule
public void schedule(TimerTask task,
Date time)
Schedules the specified task for execution at the specified time. If the time is in the past, the task is scheduled for immediate execution.

Parameters:
task - task to be scheduled.
time - time at which task is to be executed.

我想是那问题
多谢你的贴

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧