简体   繁体   English

timer.schedule变速

[英]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. 我不是计时器每2.5秒(在程序开始时)完成一项工作,即使用以下代码。

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: 速度是一个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. 但问题是计时器的速度保持在2500,而变速每次降低500,因此该部分正在工作。 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. 你不能这样做,因为一旦你完成了计划,它将用Timer修复它。

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. 在这种情况下,您可以取消上一个并安排新的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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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