Mike Warren
Mike Warren

Reputation: 3866

Is one Thread per Client the answer?

I have been poring over some research for multithreaded programming, such as this academic webpage, and have noticed that it is quite popular to create one Thread for each client that connects to a given server. In fact, I have found some sample client-server program that does just this. (I attempted to adopt the idea, but now, I am in doubt.) According to Java: How to Program, it is recommended that I use the ExecutorService to create and manage Threads since the programmer cannot predict when a Thread will actually be dispatched by the system, despite the order in which threads are created and started.


What I intend to do

As mentioned earlier, I am creating a server that creates a Thread for each client. The clients are to send data to me, and the Thread will fetch the data, store it in a file, and log the data.


My question

Would using the ExecutorService to create Threads (and manage them!) be effectively the same as giving each client a Thread , but more manageable? Also, would it eliminate the overhead caused by the famous "one-thread-per-client" idea?

Upvotes: 2

Views: 703

Answers (1)

Peter Lawrey
Peter Lawrey

Reputation: 533442

Would using the ExecutorService to create Threads (and manage them!) be effectively the same as giving each client a Thread , but more manageable?

yes

Also, would it eliminate the overhead caused by the famous "one-thread-per-client" idea?

no. The overhead is usually in terms of the number of active threads which is not changed by using a thread pool.

Upvotes: 1

Related Questions