• Post Reply Bookmark Topic Watch Topic
  • New Topic

Server: one thread per one socket?

 
Ivan Jouikov
Ranch Hand
Posts: 269
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am wondering if it's approporiate to create thread for every client that connects to a server...
I mean if you have over a thousand clients, having thousand threads would destroy your program...
Is there a different approach? I remember how Apache uses C's open() function or something that notifies one of a few threads that a client is connected, but what do I do in case of Java?
Thanks in advance?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One thread per client is a common way to handle this. As you note, it doesn't scale very well for a large number of clients. The preferred solution as of JDK 1.4 is to use a Selector, which allows a single thread to efficiently listen to a huge number of channels. That thread may try to handle every event that occurs on any channel, or you may want to use a thread pool. Whenever something happens on any channel, the Selector picks it up and hands it off to a worker thread to process the new event. That way the thread running the Selector isn't tied up, and can respond to the next event. You might have 1000 clients, but only 10% are active at one time - so one Selector and a pool of 100 worker threads can handle the job much more efficiently than 1000 separate threads could.
You can learn more in this article, and find some examples here - the last two use Selctors. If you haven't used any of the NIO classes before you'll need to spend some time understanding buffers and channels first. O'Reilly's Java NIO may be a worthwhile investment.
 
V Chauhan
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim,

You might have 1000 clients, but only 10% are active at one time

Just a doubt. In case of J2EE Web Applications, the Application Server handles it in a way similar to what you have explained. The number of threads depend on the number of active clients, i.e., the clients for which a request is being processed by the server at any point of time. App Servers such as Weblogic use thread pools for this purpose.
Pl correct me if i am wrong.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just for what it's worth ... I recall Microsoft IIS had a configurable thread pool. When all threads were busy, it put requests into a configurable queue. When the queue filled up, it returned "The server is busy" error. Seems like a sensible multi-stage approach.
 
Abdul Basit
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good answer by Jim Yingst and again a good question from basudev agrawal ,right now i have one another Question in my mind which is :
"NIO is introduce in java1.4 but the web servers those are build on java1.3 how the handle this situation " .
I mean the situation explain by Ivan Jouikov .
Any comments on this :
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!