Setting up a Multithreaded Server in Java

Malina Tran
Tech and the City
Published in
2 min readAug 25, 2016

I was delighted to start working on building an HTTP server in Java, breaking away from the tic-tac-toe component of my residency.

My latest task was to convert my server to handle multiple, concurrent requests from clients or to handle multiple client connections. Concurrency is something that we easily take for granted — so much of how we interact with computers require multiple things to happen simultaneously. The Java platform has basic concurrency support built into the language and within its libraries.

A Brief Intro

There are two units of execution: processes and threads. A process is a “self-contained execution environment” with runtime resources and its own memory space. Implementations of the Java Virtual Machine run as a single process. Meanwhile, a thread is considered a “lightweight process” in the sense that it requires fewer resources. Threads exist within processes (at least one in each). There is a main thread which has the ability to create additional threads.

The `Thread` object exists in Java and is used to control thread creation and management (e.g. instantiating `Thread` for each asynchronous task) and abstract this management from the application.

Digging deeper into multithreading, I learned of several advantages for creating additional threads for the HTTP servers, such as:

  1. Spending more time inside the `accept()` call, which means a more responsive server, and
  2. Preventing long-running requests from making servers unresponsive.

I’ve documented the process for implementing a multithread server:

Implementation

Oracle has a great tutorial on configuring a multithreaded server in Java. Below, I will detail my implementation which was a two-part process:

Set up a class that implements the `Runnable` interface. This interface is significant because it has one method, `run`, which is responsible for executing in each thread. Let’s call this `ClientHandler`. Have a constructor (line 8) and execute the `run` method for each thread (line 12).

Have a method that loops on the `server.accept()` call. Prior to that, make sure you create a new server socket (and don’t include that in the loop, unless you’d like to create a bajillion server sockets that all share the same port number — a recipe for disaster). For each client connection that is accepted, create an instance of `ClientHandler` and a corresponding thread for each instance. Don’t forget to close the server socket before the program’ shuts down by housing it in `finalize()`.

While 8th Light has its own acceptance criteria for successfully implementing a multithreaded server, I was interested in finding other ways to test on my own. Apparently, there are various ways to test whether your server can handle concurrent connections to a single URL (unfortunately, cURL isn’t one of them). I have a `req.js` file (see below for contents) and run `node req.js`. If it doesn’t spit out an error and runs the file, I’m good to go.

var http = require(‘http’)http.get(‘http://localhost:5000')
http.get(‘http://localhost:5000')
http.get(‘http://localhost:5000')

Current feeling documented below.

--

--