将参数传递给定时器任务 (Java) [英] Pass parameters to Timer Task (Java)

查看:33
本文介绍了将参数传递给定时器任务 (Java)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个循环中的计时器任务.我想将它在循环中的编号传递给时间任务.

I have a timer task in a loop. I want to pass into the time task which number it is in a loop.

这可能吗?

我的代码:

...
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 下/in/from 创建的.

How can I change it so I can use i in the TimerTask class? -as in each TimerTask will know which i it was created under/in/from.

推荐答案

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)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆