Java timer with not-fixed delay

落爺英雄遲暮 提交于 2019-12-01 06:37:18

I don't think that the java.util.Timer class supports this.

Something you can do is use the Timer.schedule(TimerTask, int) method that executes your task one after a certain amount of time. When your task gets executed, you can then scheduled a new timer with the new interval you want.

Something like:

int moveTime = 1000;

Timer timer = new Timer();

public Bot(){
    timer.schedule(new Task(), moveTime);
}

public class Task extends TimerTask {
    @Override
    public void run() {
        System.out.println("Time Passed from last repeat:"+movetime)
        moveTime += 1000;
        timer.schedule(new Task(), moveTime)
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!