How can I change my TimerTask's execution period at runtime?I

VextoR picture VextoR · Jun 29, 2011 · Viewed 11.9k times · Source

How can I change period of Timer at runtime?

    Timer timer = new Timer();

    timer.scheduleAtFixedRate(new TimerTask() {
        public void run() {

             // read new period
             period = getPeriod();

             doSomething();

        }
    }, 0, period);

Answer

Mathias Schwarz picture Mathias Schwarz · Jun 29, 2011

You cannot do this directly, but you can cancel the tasks on the Timer and reschedule them with the desired period.

There is no getPeriod method.