There's more...

The Executors class provides other methods to create ThreadPoolExecutor:

  • newCachedThreadPool(): This method returns an ExecutorService object, so it's been cast to ThreadPoolExecutor to have access to all its methods. The cached thread pool you created creates new threads, if needed, to execute new tasks. Plus, it reuses the existing ones if they have finished the execution of the tasks they were running.
  • newSingleThreadExecutor(): This is an extreme case of a fixed-size thread executor. It creates an executor with only one thread so it can only execute one task at a time.

The ThreadPoolExecutor class provides a lot of methods to obtain information about its status. We used the getPoolSize(), getActiveCount(), and getCompletedTaskCount() ...

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.