Java socketserver: how to handle many incoming connections?

I am writing a simple multi-threaded socketserver, and I am wondering how best to handle incoming connections:

  • create a new thread for each new connection. The number of parallel threads will be limited and connection expectations are limited to indicate lag

  • add all incoming connections to the queue and create a pool of workflows that process the queue

I tend to go for option 2 because I really don't want to give up any connections, even under high loads, but I wonder if there are any considerations I should be aware of when taking effective unlimited connections?

+5
source share
5 answers

With unlimited connections, you can potentially create a huge number of threads. This means that a lot of processing is required, and each thread will consume a fixed amount of memory by default only for the heap (I think the figure is 512 kB per thread, but it depends on the platform).

By combining a fixed number of threads and accepting a limited number of clients, you guarantee that some of your clients will be served for a reasonable period of time, and your server will not crash during congestion.

You can check out this article about building servers using NIO, or maybe check out frameworks like Apache Mina .

+6
source

-

, , , . , , , . , .

, 70% . , , .

, , NIO. , - .

+4

, , , , , , , .

+2

1 . C10K. , .

NIO , , netty, (NIO) .

+2

- ,

2 - , . NIO 5-10k. , DEMUX , . MUX, . EJP, , , , . , , . , :

enter image description here

Disclaimer: I am one of the CoralQueue developers

0
source

All Articles