6

Currently I am using a timer to execute some function every interval time. However, later on when I want to change the interval of executing the function, I cannot cancel the previous schedule. how this can be solved? Thanks

user301
  • 91
  • 1
  • 2
  • 7
  • what do you mean "i cannot cancel the previous schedule"? do you not know how to do it, or is it that it does not fit your architecture? – njzk2 Oct 27 '11 at 09:19
  • I want to cancel the previous schedule and start new schedule with different interval. However, for timer, there seems no way to do this..so frustrating – user301 Oct 28 '11 at 04:19
  • you have to actually cancel the timer and recreate one – njzk2 Nov 02 '11 at 16:30
  • Why? What's the purpose of TimerTask#cancel then? – JohnyTex Feb 17 '16 at 10:46

2 Answers2

5

With the timer.cancel() method you can cancels the Timer and all scheduled tasks. (see API documentation) or you can call the cancel method on your TimerTask timertask.cancel() (see API documentation)

If you want to change the scheduled time you should cancel the TimerTask and add a new TimerTask.

Mats Hofman
  • 7,060
  • 6
  • 33
  • 48
3

You could look into using a ScheduledThreadPoolExecutor instead of a Timer.

Usage is pretty straight forward. You create an instance of an executor:

ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor( 1 );

And then when you want to add a task you call:

executor.scheduleAtFixedRate( myRunnable, delay, interval, unit );

Where myRunnable is your task (which implements the Runnable-interface), delay is how long before the task should be executed the first time, interval is time between the execution of the task after first execution. delay and interval are meassured based on the unit parameter, which can be TimeUnit.* (where * is SECONDS, MINUTES, MILLISECONDS etc.).

Then to stop the execution you call:

executor.shutdownNow();

And then you can re-submit your task with a different interval.

Note: You might need to create a new instance of the executor before resubmitting your task, but I'm not quite sure why.

kaspermoerch
  • 16,127
  • 4
  • 44
  • 67
  • how is it different from a timer? – njzk2 Oct 27 '11 at 09:18
  • This link tells you a little about the differences: http://www.dremsus.com/index.php/2011/03/why-scheduledthreadpoolexecutor-for-smartfoxserver-2x/ Also this question on Stackoverflow adresses the differences: http://stackoverflow.com/questions/409932/java-timer-vs-executorservice – kaspermoerch Oct 27 '11 at 09:24
  • is there anyway that I don't need to shut down the executor or close the timer? I don't want to create a new instance of the executor or timer object... – user301 Oct 28 '11 at 04:01
  • You could try experimenting with the `remove(Runnable task)` method of the `ScheduledThreadPoolExecutor`. It removes a task, that has not yet been run. The reason I say you should "experiment" is that I'm not sure if it works with regards to task scheduled at a fixed rate. – kaspermoerch Oct 28 '11 at 06:01