Doc
Doc

Reputation: 11

Java NIO multiplexed Server: should I use worker threads to process requests?

Should I accept connections and monitoring clients on a listener thread and then let workers handle the request and answer to the client, or should I do everything on one thread?

Upvotes: 1

Views: 515

Answers (2)

Matt Timmermans
Matt Timmermans

Reputation: 59154

Neither.

Ideally, for an NIO-based server, you create a thread pool using something like Executors.newFixedThreadPool(), which you will use to perform all the processing for handling your requests.

But, there should be no assignment of requests to specific threads, because the rest of your system should be asynchronous as well. That means that when a request handler needs to perform some lengthy I/O work or similar, instead of blocking the thread and waiting for it to finish, it starts it asynchronously and arranges for processing to continue when the work is finished by submitting a new task to the thread pool. There's no telling which thread will pick up the work at that point, so the processing for a request could end up being spread across many threads.

You should usually coordinate your asynchronous processing using CompletableFuture the same way that Promise is used in node. Have a look at my answer over here, which tries to explain how to do that: decoupled design for async http request

Upvotes: 2

rustyx
rustyx

Reputation: 85286

If your request handling is 100% asynchronous, that is you never wait for anything during request handling and you're on a single-core system, then it might be slightly better to do everything in the same thread.

If you have a multi-core system or you wait on I/O during request processing, then you should use a thread pool instead.

Upvotes: 1

Related Questions