How it works...

When you want to execute a periodic task using the Executor framework, you need a ScheduledExecutorService object. To create it (as with every executor), Java recommends the use of the Executors class. This class works as a factory of executor objects. In this case, you used the newScheduledThreadPool() method to create a ScheduledExecutorService object. This method receives the number of threads of the pool as a parameter. Since you had only one task in this example, you passed 1 as a parameter.

Once you had the executor needed to execute a periodic task, you sent the task to the executor. You used the scheduledAtFixedRate() method. This method accepts four parameters: the task you want to execute periodically, the delay ...

Get Java 9 Concurrency Cookbook - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.