javatimertimertask

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


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);

Solution

  • 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.