From the course: Advanced Java: Threads and Concurrency

Unlock the full course today

Join today to access over 23,200 courses taught by industry experts.

Executing tasks periodically with ScheduledExecutorService

Executing tasks periodically with ScheduledExecutorService - Java Tutorial

From the course: Advanced Java: Threads and Concurrency

Executing tasks periodically with ScheduledExecutorService

- [Instructor] We might not want to execute tasks immediately or as soon as possible all the time. We might want to schedule tasks in our applications for execution when we want. There are mainly two ways in which we may want to schedule tasks, execute a task after a period of time, or execute a task periodically. This is where we need the ScheduledExecutorService. It's another extension of the executor, like the ExecutorService. This interface has three important methods, the overloaded schedule methods that take in a runnable or callable task along with a delay so that the task executes after the given delay in time. ScheduledAtFixedRate method that lets us start a task after an initial delay, and then repeat execution at the given rate or period. If any execution takes longer than the given period, subsequent executions may start late after waiting for the previous run to complete. ScheduleWithFixedDelay method is similar to scheduleAtFixedRate method, but lets us have a…

Contents