I\'m writing a Java server which uses plain sockets to accept connections from clients. I\'m using the fairly simple model where each connection has its own thread reading f
If you have any interest in leveraging deployment and management of an existing container, you might look at making a new protocol handler inside of Tomcat. See this answer to a related question.
UPDATE: This post from Matthew Schmidt claims the NIO-based connector (written by Filip Hanik) in Tomcat 6 achieved 16,000 concurrent connections.
If you want to write your own connector, take a look at MINA to help with NIO abstractions. MINA also has management features which may eliminate need for another container (should you be concerned about deployment of many units and their operation, etc.)
The JVM for Linux is using one to one thread mapping. This means that every Java thread is mapped to one native OS thread.
So creating a thousand of threads or more is not a good idea because it will impact your performance (context switching, cache flushes / misses, synchronization latency etc). It also doesn't make any sense if you have less than a thousand of CPUs.
The only adequate solution for the serving many clients in parallel is to use asynchronous I/O. Please see this answer on Java NIO for details.
See also:
To have a good perfomance when handling many sockets you usually use a select
approach that is how Unix API handles single-threaded multi-socket applications that need many resources.
This can be done through the java.nio
package that has a Selector
class which basically is able to go through all the opened sockets and notify you when new data is available.
You register all the opened streams inside a single Selector
and then you can handle all of them from just one thread.
You can get additional infos with a tutorial here
Why roll your own? You could use a servlet container with servlets, a message queue, or ZeroMQ.