简体   繁体   中英

timer.schedule variable speed

i wan't a timer do a job every 2.5 seconds (at the start of the program), that is working 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 a int:

public int Speed=2500;

buth the problem is that the speed of the timer stays on the 2500, while the variable speed lowers each time with 500, so that part is working. Only the timer doesn't check if Speed has changed.

Can somebody help me with this?

you cant do that because it will fix that with Timer once you done the schedule.

Schedules the specified task for repeated fixed-delay execution, beginning after the specified delay. Subsequent executions take place at approximately regular intervals separated by the specified period.

In this case you can cancel the previous one and schedule 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM