简体   繁体   中英

Managing multiple outgoing TCP connections

My program needs to send data to multiple (about 50) "client" stations. Important bits of data must be sent over TCP to ensure arrival. The connections are mostly fixed and are not expected to change during a single period of activity of the program.

What do you think would be the best architecture for this? I've heard that creating a new thread per connection is generally not recommended, but is this recommendation valid when connections are not expected to change? Scalability would be nice to have but is not much of a concern as the number of client stations is not expected to grow.

The program is written in Java if it matters.

Thanks,

Alex

If scalability, throughput and memory usage are not a concern, then using 50 threads is an adequate solution. It has the advantage of being simple, and simplicity is a good thing.

If you want to be able to scale, or you are concerned about memory usage (N threads implies N thread stacks) then you need to consider an architecture using NIO selectors. However, the best architecture probably depends on things like:

  • the amount of work that needs to be performed for each client station,
  • whether the work is evenly spread (on average),
  • whether the work involves other I/O, access to shared data structures, etc and
  • how close the aggregate work is to saturating a single processor.

50 threads is fine, go for it. It hardly matters. Anything over 200 threads, start to worry..

I'd use thread pool anyway. Depending on your thread pool configuration it will create as many threads as you need but this solution is more scalable. It will be ok not only for 50 but also for 5000 clients.

为什么不通过使用像连接池这样的东西限制线程数量?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM