Pass parameters to Timer Task (Java)

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++;
}
...

Give the RegrowCornAnimate class a constructor that takes an int and store that in a field. Pass i to the constructor when you create it.


Create a constructor in RegrowCornAnimate taking the parameters you'd like to use, then store them as members inside your class.

When RegrowCornAnimate.run is called read the values.

Tags:

Timer

Java