0

Here is the code snippet:

Timer t = new Timer();
TimerTask task = new TimerTask() {

    @Override
    public void run() {
        //change the timer rate of scheduleAtFixedRate here
    }

};

//every 10 sec
t.scheduleAtFixedRate(task, new Date(), 10000);

Could anyone tell me how to change the rate of timer to t.scheduleAtFixedRate(task, new Date(), 30000) in method run from TimerTask instance?

Thanks a lot!

Judking
  • 6,111
  • 11
  • 55
  • 84

2 Answers2

0

You can't modify an existing schedule, but you can cancel() the TimerTask that's executing, and reschedule it for the new period.

Ryan Stewart
  • 126,015
  • 21
  • 180
  • 199
0

There is no option to reschedule the timer that is already running. You need to cancel the current task and reschedule it with your new interval.

Pausing/stopping and starting/resuming Java TimerTask continuously?

Community
  • 1
  • 1
Prabhakaran Ramaswamy
  • 25,706
  • 10
  • 57
  • 64