Hi, I was writing a program for a Chat Server .... now by using threads, the server can possibly handle about 700 to 800 clients. Each client has a thread running on its behalf. this is b coz a single runtime can handle just as many threads . Now my question is : Is there a principle or a method by which i have the server handle more number of clients connecting ? may be a few thousand clients... thanks a lot regards Merwin
Joined: Oct 12, 2000
There is a limit to the number of threads a single JVM can handle. This is imposed by the hardware mainly (at some point the JVM will be so busy managing all those threads that it doesn't get to run any of them). There's nothing you can do about that, you'll have to start thinking about distributed computing. Have multiple servers talking to each other, each handling part of the load. For each server all the others are a special kind of user...
Joined: Jan 29, 2003
I'd go back and look at your design. Do you need a thread per user? Or a thread per incoming message? Here's typical server logic:
Now you need only as many threads as you have SIMULTANEOUS requests. But if traffic is high you're still setting up and tearing down a lot of threads, which can hurt performance. That leads us to look at thread pools. JDK 5.0 has great pooling stuff under the Executor interface. Then the logic looks like:
If there is an available thread, the thread will pull the request out of the queue and process it immediately. Otherwise the request will wait in queue until a thread finishes what it's doing and gets another from the queue.
Does that sound promising?
[ EJFH: Fixed mismatched CODE tags. ] [ February 18, 2005: Message edited by: Ernest Friedman-Hill ]
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi