简体   繁体   English

管理多个传出TCP连接

[英]Managing multiple outgoing TCP connections

My program needs to send data to multiple (about 50) "client" stations. 我的程序需要将数据发送到多个(约50个)“客户端”站。 Important bits of data must be sent over TCP to ensure arrival. 必须通过TCP发送重要的数据位以确保到达。 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. 如果重要的话,程序是用Java编写的。

Thanks, 谢谢,

Alex 亚历克斯

If scalability, throughput and memory usage are not a concern, then using 50 threads is an adequate solution. 如果不考虑可伸缩性,吞吐量和内存使用率,那么使用50个线程是一个合适的解决方案。 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. 如果您希望能够扩展,或者您担心内存使用(N个线程意味着N个线程堆栈),那么您需要考虑使用NIO选择器的架构。 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 工作是否涉及其他I / O,访问共享数据结构等
  • how close the aggregate work is to saturating a single processor. 聚合工作与单个处理器饱和的接近程度。

50 threads is fine, go for it. 50线程很好,去吧。 It hardly matters. 这几乎不重要。 Anything over 200 threads, start to worry.. 超过200个线程,开始担心..

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. 它不仅适用于50个客户端,也适用于5000个客户端。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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