Java Reference
In-Depth Information
6.2.1. Example: Web Server Using Executor
Building a web server with an Executor is easy. TaskExecutionWebServer in List-
ing 6.4 replaces the hard-coded thread creation with an Executor . In this case, we use one
of the standard Executor implementations, a fixed-size thread pool with 100 threads.
In TaskExecutionWebServer , submission of the request-handling task is decoupled
from its execution using an Executor , and its behavior can be changed merely by substitut-
ing a different Executor implementation. Changing Executor implementations or con-
figuration is far less invasive than changing the way tasks are submitted; Executor config-
uration is generally a one-time event and can easily be exposed for deployment-time config-
uration, whereas task submission code tends to be strewn throughout the program and harder
to expose.
Listing 6.4. Web Server Using a Thread Pool.
We can easily modify TaskExecutionWebServer to behave like ThreadPer-
TaskWebServer by substituting an Executor that creates a new thread for each request.
Search WWH ::




Custom Search