Reputation: 47
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?
Upvotes: 4
Views: 507
Reputation: 10943
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);
Upvotes: 3