Threads and Multiple Clients
I'm working on an online strategy game and I'm almost done with the difficult parts, but I just had a friend point out that my server is going to be really slow. Right now I start a seperate thread to listen to every client and each of those threads starts a seperate "function" thread (TimerTask) to deal with whatever processes the client input tell it is needs to do (Research, Upgrades, Unit Construction, etc.). I already figured out a way to make it so that I only need one thread for each "function" but I was wondering if there was any way to use only one thread for all the clients too. I have to have a minimum of 160 people running on one server and that just seems like a whole lot of threads. Am I underestimating the performance of threads or is there a better way of doing this? Also each client thread is an infinite while loop waiting for input if that makes any difference.
[900 byte] By [
kirjavaa] at [2007-10-2 3:24:09]

it's really just the method of having multiple clients connect to the server program that I am questioning here. It's just simple socket connections.
As for db it's a mysql db, but I really can't see how that plays into it.
I really don't know what I plan on running it on yet, right now it is just running on my computer but it doesn't really matter where it ends up running, I am still trying to get optimum performance with the least possible number of threads. I'm am really pretty new at this, everything I've learned about java I taught myself while making this game so my knowledge is very specific.
> I'm working on an online strategy game and I'm almost
> done with the difficult parts, but I just had a
> friend point out that my server is going to be really
> slow.
Based upon what? Is this friend an expert in the field?
> Right now I start a seperate thread to listen
> to every client and each of those threads starts a
> seperate "function" thread (TimerTask) to deal with
> whatever processes the client input tell it is needs
> to do (Research, Upgrades, Unit Construction, etc.).
> I already figured out a way to make it so that I only
> need one thread for each "function" but I was
> wondering if there was any way to use only one thread
> for all the clients too. I have to have a minimum of
> 160 people running on one server and that just seems
> like a whole lot of threads. Am I underestimating the
> performance of threads or is there a better way of
> doing this?
I don't think running 160 threads is necessarily going to be slow. It mostly depends on the amount of contention (multiple threads requiring the same resources at the same time.) Threads themselves are pretty lightweight.
> Also each client thread is an infinite
> while loop waiting for input if that makes any
> difference.
Just make sure you add a short sleep in the loop so they don't pin the CPU at 100%.
> it's really just the method of having multiple
> clients connect to the server program that I am
> questioning here. It's just simple socket
> connections.
Using [url=http://java.sun.com/j2se/1.4.2/docs/api/java/nio/channels/package-summary.html]java.nio.channels[/url] you can handle all events on your client sockets in one thread.
> As for db it's a mysql db, but I really can't see how that plays into it.
I found no mention of DB at all worrying.
You need to get your database structure right.
> I really don't know what I plan on running it on yet,
> right now it is just running on my computer but it
> doesn't really matter where it ends up running, I am
> still trying to get optimum performance with the
> least possible number of threads.
One thread per processor for least overhead - but this does not necessarily give the best user experience - have a look at how J2EE Application Servers use thread pools.