开发者

Managing multiple outgoing TCP connections

开发者 https://www.devze.com 2023-02-12 19:36 出处:网络
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 e开发者_JAVA技巧x

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 e开发者_JAVA技巧xpected 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.


Why don't you limit the amount of threads by using someting like a connection Pool?

0

精彩评论

暂无评论...
验证码 换一张
取 消