I’d like to have a java.utils.Timer with a resettable time in java.I need to set a once off event to occur in X seconds. If nothing happens in between the time the timer was created and X seconds, then the event occurs as normal.
If, however, before X seconds has elapsed, I decide that the event should occur after Y seconds instead, then I want to be able to tell the timer to reset its time so that the event occurs in Y seconds. E.g. the timer should be able to do something like:
Timer timer = new Timer(); timer.schedule(timerTask, 5000); //Timer starts in 5000 ms (X) //At some point between 0 and 5000 ms... setNewTime(timer, 8000); //timerTask will fire in 8000ms from NOW (Y).
I don’t see a way to do this using the utils timer, as if you call cancel() you cannot schedule it again.
The only way I’ve come close to replicating this behavior is by using javax.swing.Timer and involves stopping the origional timer, and creating a new one. i.e.:
timer.stop(); timer = new Timer(8000, ActionListener); timer.start();
Is there an easier way??
According to the
Timerdocumentation, in Java 1.5 onwards, you should prefer theScheduledThreadPoolExecutorinstead. (You may like to create this executor usingExecutors.newSingleThreadScheduledExecutor()for ease of use; it creates something much like aTimer.)The cool thing is, when you schedule a task (by calling
schedule()), it returns aScheduledFutureobject. You can use this to cancel the scheduled task. You’re then free to submit a new task with a different triggering time.ETA: The
Timerdocumentation linked to doesn’t say anything aboutScheduledThreadPoolExecutor, however the OpenJDK version had this to say: