Server in Java

Programming, for all ages and all languages.
Post Reply
User avatar
Neo
Member
Member
Posts: 842
Joined: Wed Oct 18, 2006 9:01 am

Server in Java

Post 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?
Only Human
User avatar
Colonel Kernel
Member
Member
Posts: 1437
Joined: Tue Oct 17, 2006 6:06 pm
Location: Vancouver, BC, Canada
Contact:

Re:Server in Java

Post 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.
Top three reasons why my OS project died:
  1. Too much overtime at work
  2. Got married
  3. My brain got stuck in an infinite loop while trying to design the memory manager
Don't let this happen to you!
User avatar
Neo
Member
Member
Posts: 842
Joined: Wed Oct 18, 2006 9:01 am

Re:Server in Java

Post by Neo »

Ok so would the Server be better off in a separate thread (from the main thread) or not?
Only Human
User avatar
Colonel Kernel
Member
Member
Posts: 1437
Joined: Tue Oct 17, 2006 6:06 pm
Location: Vancouver, BC, Canada
Contact:

Re:Server in Java

Post 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.
Top three reasons why my OS project died:
  1. Too much overtime at work
  2. Got married
  3. My brain got stuck in an infinite loop while trying to design the memory manager
Don't let this happen to you!
Post Reply