How it works...

The key point of this example is the Main class and the management of ScheduledThreadPoolExecutor. As with the ThreadPoolExecutor class, to create a scheduled executor, Java recommends that you utilize the Executors class. In this case, you used the newScheduledThreadPool() method. You passed the number 1 as a parameter to this method. This parameter refers to the number of threads you want to have in the pool.

To execute a task in this scheduled executor after a period of time, you have to use the schedule() method. This method receives the following three parameters:

  • The task you want to execute
  • The period of time you want the task to wait before its execution
  • The unit of the period of time, specified as a constant of the ...

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.