There's more...

You can also use the Runnable interface to implement the tasks because the schedule() method of the ScheduledThreadPoolExecutor class accepts both types of tasks.

Although the ScheduledThreadPoolExecutor class is a child class of the ThreadPoolExecutor class (and therefore inherits all its features), Java recommends that you use ScheduledThreadPoolExecutor only for scheduled tasks.

Finally, you can configure the behavior of the ScheduledThreadPoolExecutor class when you call the shutdown() method, and there are pending tasks waiting for the end of their delay time. The default behavior is that these tasks will be executed despite the finalization of the executor. You can change this behavior using the setExecuteExistingDelayedTasksAfterShutdownPolicy() ...

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.