O'Reilly logo

Java 9 Concurrency Cookbook - Second Edition by Javier Fernández González

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required