I'm using a ScheduledExecutorService
to run a particular job (implemented as an ordinary Runnable
) periodically once in a minute (using method scheduleAtFixedDelay()
).
Occasionally, however, I would like it to wake up immediately, invoke the Runnable
and then return to its ordinary policy (i.e. wait 1 minute again).
Is there a simple way to achieve this?
I've checked the API of the ScheduledExecutorService
and its superclasses, but so far didn't find anything suitable.
Of course I could resort to some other method, like pass the same Runnable
to a separate Thread created for the exceptional purpose, but using a method of the ScheduledExecutorService
would be more elegant.
Just remember the ScheduledFuture from your call to schedule.
If you then want to run it ahead of time, call future.cancel(), submit the Task again for immediate execution and then schedule it again.