How can I change the timer period at runtime?
Timer timer = new Timer(); timer.scheduleAtFixedRate(new TimerTask() { public void run() { // read new period period = getPeriod(); doSomething(); } }, 0, period);
You cannot do this directly, but you can cancel tasks on Timer and transfer them with the required period.
Timer
There is no getPeriod method.
getPeriod
You can do it as follows:
private int period= 1000; private void startTimer() { Timer timer = new Timer(); timer.schedule(new TimerTask() { public void run() { // do something... System.out.println("delay = " + delay); period = 500; // change the period time timer.cancel(); // cancel time startTimer(); // start the time again with a new delay time } }, 0, period); }