One thread per client. Doable?

后端 未结 10 1749
走了就别回头了
走了就别回头了 2020-12-15 18:43

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

相关标签:
10条回答
  • 2020-12-15 19:36

    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.)

    0 讨论(0)
  • 2020-12-15 19:38

    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:

    • Green threads
    • Solaris threading models
    0 讨论(0)
  • 2020-12-15 19:39

    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

    0 讨论(0)
  • 2020-12-15 19:42

    Why roll your own? You could use a servlet container with servlets, a message queue, or ZeroMQ.

    0 讨论(0)
提交回复
热议问题