Page 1 of 1

Server in Java

Posted: Sat Apr 30, 2005 11:42 pm
by Neo
Which is a better method of handling threads in a server.
1.I keep accepting clients in this main function and spawning new threads for each client.

2.I have the main infinite loop in a separate thread and also spawn a new thread for each client.

What are the pros and cons of each method?

Re:Server in Java

Posted: Sun May 01, 2005 12:06 am
by Colonel Kernel
I'm not sure I understand the distinction you're making between 1 and 2, but I don't think either one is a good idea. Spawning new threads can be expensive, so you should avoid creating them for every client request. It's better to maintain a pool of threads and assign them to client requests as they come in. The pool would only grow if necessary, but it would do so in a controlled manner.

Maybe Java already has thread pooling in the APIs somewhere -- if so I'm not aware of it. I've only used thread pooling in .NET.

Re:Server in Java

Posted: Sun May 01, 2005 12:18 am
by Neo
Ok so would the Server be better off in a separate thread (from the main thread) or not?

Re:Server in Java

Posted: Sun May 01, 2005 8:55 am
by Colonel Kernel
I don't see what difference it makes, unless the main thread has other things it should be doing besides controlling the server as a whole.