Forum OpenACS Q&A: socket connection pooling (Thread pooling)

Can anyone help me to explain abt Thread pooling.
Collapse
Posted by kiran kadarla on
Hi Jegan
  I tryto explain Therad Polling here.
See the below code which implements a multi threaded server.
----
public static void main(String args[]) throws Exception {
  ServerSocket servSoc = new ServerSocket(2345);
  while (true) {
  try {
    Socket soc = servSoc.accept();
    ClientServerObject cns = new ClientServerObject(soc);
    cns.start();
  }
    catch (IOException ex) {}
  }
}
***
  There are two issue with the above code.
1. Every time you pass through this loop, a new thread gets created. Every time a connection is finished the thread is disposed of.
2.Spawning a new thread for each connection takes a non-trivial amount of time, especially on a heavily loaded server. It would be better not to spawn so many threads.
***
Solution to the above problem is Thread Pooling.

1.Create a pool of threads when the server launches, store incoming connections in a queue, and have the threads in the pool progressively remove connections from the queue and process them.
2. The main change you need to make to implement this is to call accept() in the run() method rather than in the main() method.
  I think from here you can do it urself..

regards
kiran kadarla