How it works...

The key of this example is the Server class. This class creates and uses ThreadPoolExecutor to execute tasks.

The first important point is the creation of ThreadPoolExecutor in the constructor of the Server class. The ThreadPoolExecutor class has four different constructors, but due to their complexity, the Java concurrency API provides the Executors class to construct executors and other related objects. Although you can create ThreadPoolExecutor directly using one of its constructors, it's recommended that you use the Executors class.

In this case, you created a cached thread pool using the newFixedThreadPool() method of the Executors class in order to create the executor. This method creates an executor with the maximum ...

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.