How it works...

In this recipe, we implemented our custom executor by extending the ThreadPoolExecutor class and overriding four of its methods. The beforeExecute() and afterExecute() methods were used to calculate the execution time of a task. The beforeExecute() method is executed before the execution of a task; in this case, we used HashMap to store the start date of the task in it. The afterExecute() method is executed after the execution of the task. You got startTime of the task that had finished from HashMap and then calculate the difference between the actual date and and the startTime to get the execution time of the task. You also overrode the shutdown() and shutdownNow() methods to write statistics about the tasks executed in 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.