我有一个循环中的计时器任务。我想将循环中的数字传递给时间任务。
这可能吗?
我的代码:
...
int i = 0;
while (i < array.size){
Timer timer = new Timer();
timer.schedule(new RegrowCornAnimate(), 0, 1000);
i++
}
...
class RegrowCornAnimate extends TimerTask {
public void run() {
//Do stuff
}
}
如何更改它以便我可以在 TimerTask 类中使用 i
? - 因为在每个 TimerTask 中都会知道它是在哪个 i
下/在/从中创建的。
最佳答案
class RegrowCornAnimate extends TimerTask {
private final int serial;
RegrowCornAnimate ( int serial )
{
this.serial = serial;
}
public void run() {
//Do stuff
}
}
...
int i = 0;
while (i < array.size){
Timer timer = new Timer();
timer.schedule(new RegrowCornAnimate( i ), 0, 1000);
i++;
}
...
关于java - 将参数传递给定时器任务 (Java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8498691/