【发布时间】:2016-10-03 11:53:12
【问题描述】:
我有一个定时器任务如下图:
long timerSec = 5000;
TimerTask task1 = new TimerTask()
{
public void run()
{
//Some code...
System.out.println("Timer task...");
}
};
还有一个定时器对象如下图:
Timer readFileTimer = new Timer();
我在两个连续的任务执行之间安排了一个间隔 5 秒的任务。
readFileTimer.scheduleAtFixedRate(task1, 0, timerSec);
下面的代码行分配了新的时间段。我想将时间段从 5 秒更改为 n 秒(让我们说 10 秒 w.r.t. timerSec 值)。
timerSec = CalculateTimeForUpgrade(); //Get new timer interval period.
我尝试了下面的代码,但没有得到预期的结果。
readFileTimer.scheduleAtFixedRate(task1, 0, timerSec);
请帮忙。提前致谢。
【问题讨论】: