O'Reilly logo

Java 9 Concurrency Cookbook - Second Edition by Javier Fernández González

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required