Customizing the ThreadPoolExecutor class
The Executor
framework is a mechanism that allows you to separate thread creation from its execution. It's based on the Executor
and ExecutorService
interfaces with the ThreadPoolExecutor
class that implements both the interfaces. It has an internal pool of threads and provides methods that allow you to send two kinds of tasks and execute them in the pooled threads. These tasks are:
- The
Runnable
interface to implement tasks that don't return a result - The
Callable
interface to implement tasks that return a result
In both cases, you only send the task to the executor. The executor uses one of its pooled threads or creates a new one to execute those tasks. It also decides the moment in which the task is executed.
In this recipe, you will learn how to override some methods of the ThreadPoolExecutor
class to calculate the execution time of the tasks that you will execute in the executor and write about the executor in console statistics when it completes its...