Member-only story
From 1 to 100 000 Requests in Java: 4 Hour Journey Scaling using Threads, NIO, Netty, and Virtual Threads
7 min readJan 3, 2025
Multi-Threaded Server with Blocking I/O
When a thread tries to read data from a socket, it waits/blocks until some data is available or happened a time out. To know if data is already available you need to read the data. So, a thread that handles a connection must:
- Request to read data
- Block until data is available
- Process the request
- Send back the response
With Blocking I/O, each client connection requires its own server thread. For example, if the server has only 20 threads and all threads are busy handling client requests, new requests will have to wait until a thread becomes available.
Below is the implementation:
public class MultiThreadedBlockingServer {
public static void main(String[] args) {
int port = 9997;
int backlog = 30_000;
int nrThreads = 30;
ExecutorService executorService = Executors.newFixedThreadPool(nrThreads);
try(ServerSocket serverSocket = new ServerSocket(port, backlog)) {
System.out.println("Server is listening on port " + port);
while (true) {
Socket clientSocket = serverSocket.accept();
executorService.execute(() -> handleConnection(clientSocket));
}
} catch (IOException e) {…