我想知道这门课的问题在哪里,我正在做一门课,每隔 n 秒就会做一些事情,但它似乎只做 1 次。这是课
import java.util.Timer;
import java.util.TimerTask;
public class Updater {
private Timer timer;
public Updater(int seconds){
timer = new Timer();
timer.schedule(new UpdaterTask(), seconds*1000);
}
class UpdaterTask extends TimerTask {
public void run() {
System.out.println(Math.random());
timer.cancel();
}
}
}
这是测试
public class TestUpdater {
public static void main(String[] args){
new Updater(1);
}
}
我认为这个测试必须每秒给我一个随机数,但在第一秒之后进程终止。抱歉英语不好,感谢任何建议