Using the HttpServer executor
We introduced the HTTPServer
class in Chapter 4, Client/Server Development. When the HTTP Server receives a request, by default, it uses the thread that was created when the start
method is called. However, it is possible to use a different thread. The setExecutor
method specifies how these requests are assigned to threads.
The argument of this method is an Executor
object. We can use any of several implementations for this argument. In the following sequence, a cached thread pool is used:
server.setExecutor(Executors.newCachedThreadPool());
To control the number of threads that are used by the server, we can use a fixed thread pool of size 5
, as shown here:
server.setExecutor(Executors.newFixedThreadPool(5));
This method must be called before the start
method of HTTPServer
is called. All requests are then submitted to the executor. The following is duplicated from the HTTPServer
class that was developed in Chapter 4, Client/Server Development, and...