The second example – executing periodic tasks
In the previous examples with executors, the tasks were executed once, and they were executed as soon as possible. The executor framework includes other executor implementation that gives us more flexibility about the execution time of the tasks. It's the ScheduledThreadPoolExecutor
class that allows us to execute tasks periodically and to execute tasks after a delay.
In this section, you will learn how to execute periodic tasks implementing a RSS feed reader. This is a simple case where you need to make the same task (reading the news of a RSS feed) at regular intervals. Our example will have the following characteristics:
- Store the RSS sources in a file. We have chosen news about the world from some important newspapers, such as The New York Times, the Daily News, or The Guardian.
- We sent a
Runnable
object to the executor per RSS source. Every time the executor runs the object, it parses the RSS source and converts it to a list of...