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?
Server in Java
- Colonel Kernel
- Member
- Posts: 1437
- Joined: Tue Oct 17, 2006 6:06 pm
- Location: Vancouver, BC, Canada
- Contact:
Re:Server in Java
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.
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:
- Too much overtime at work
- Got married
- My brain got stuck in an infinite loop while trying to design the memory manager
Re:Server in Java
Ok so would the Server be better off in a separate thread (from the main thread) or not?
Only Human
- Colonel Kernel
- Member
- Posts: 1437
- Joined: Tue Oct 17, 2006 6:06 pm
- Location: Vancouver, BC, Canada
- Contact:
Re:Server in Java
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:
- Too much overtime at work
- Got married
- My brain got stuck in an infinite loop while trying to design the memory manager