Timer.schedule variable speed

i timer runs every 2.5 seconds (at the beginning of the program), which works with the following code.

Timer timer = new Timer(); timer.schedule(new TimerTask() { // @Override @Override public void run() { here is the code, and i do Speed = Speed-500 }, Speed,Speed); 

Speed ​​is int:

 public int Speed=2500; 

but the problem is that the timer speed remains at 2500, and the variable speed decreases each time by 500, so that part works. Only the timer does not check if the speed has changed.

Can someone help me with this?

+4
source share
1 answer

you cannot do this because it will fix it with a Timer after completing the schedule.

Schedules the specified task for repeated execution with a fixed delay, starting from the specified delay. Subsequent executions are carried out at approximately equal intervals divided by the indicated period.

In this case, you can undo the previous one and schedule a new TimerTask.

 Timer timer = new Timer(); initialize the speed here loop based on time timer.schedule(new TimerTask() { // @Override @Override public void run() { here is the code, and i do Speed = Speed-500 }, Speed,Speed); 
+3
source

All Articles