The advanced reader

The basic news reader is an example of the utilization of a ScheduledThreadPoolExecutor class, but we can go a step further. As occurs with ThreadPoolExecutor, we can implement our own ScheduledThreadPoolExecutor to obtain a particular behavior. In our case, we want the delay time of our periodic task changes to depend on the moment of the day. In this part, you will learn how to implement this behavior.

The first step is to implement a class that tells us the delay between two executions of a periodic task. We named this the Timer class. It only has a static method named getPeriod() that returns the number of milliseconds between the end of one execution and the start of the next one. This is our implementation, but ...

Get Mastering Concurrency Programming with Java 9 - 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.