The difference between this methods is as follows:
ScheduledExecutorService.schedule
When calling this method you should define a Runnable
that is your task, the time the action is to be executed and TimeUnit (if and hoars, minutes, etc.). This method will perform what we call one-shot action . Your runnable will only run once in the defined time. (There are overloaded versions that accept Calleables instead of Runnables)
ScheduledExecutorService.scheduleAtAFixedRate
Already in the call of scheduleAtAFixedRate
m [all Creates and executes an action - Runnable/Callable
periodic that is activated for the first time after the given initial delay. If any execution of the task throws an exception, subsequent executions are suppressed, ie they are not executed. Otherwise, the task will only terminate via cancellation or shutidown of executor
.
Take note of this detail:
If any execution of this task takes more time than its period, then subsequent executions can be started later, but there will be no concurrency execution.
Please take a look at JavaDocs
link