在多线程服务器应用程序中哪种设计模式更好? [英] which better design pattern in muti-threads server application ?

查看:69
本文介绍了在多线程服务器应用程序中哪种设计模式更好?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨每一个

哪个更好的多线程应用程序设计模式可以处理成千上万的请求分钟

哪个更好地创建接受连接的线程并创建单独的线程

处理每个客户端的响应或将此客户端设置在队列中并处理请求

hi every one
which better design pattern for multi thread application handle thousands on requests at minute
and which better create thread for accept connection and create individual threads
to handle response of every client or set this clients in queue and handle requests

推荐答案

并非所有问题都能通过(现有)设计模式得到最好的解决。你会对Go4发布设计模式之前开发的程序数量感到惊讶。



这就是说多线程不是在上述书籍发布之前的一个主题,我确信很多模式可以合理地应用于多线程应用程序的某些领域。然而,就像你可以在任何地方廉价购买的普通产品一样,设计用于普通用户,设计模式最好用于具有平均要求的环境中。



你有强烈的要求,我。即实施高性能系统,这些要求总是需要根据具体问题和情况量身定制!您可以根据自己的要求调整设计模式,但不知道这些要求到底是什么,也不可能说这是否真的值得你花时间。最有可能的是,你最好不要浪费时间将圆钉图案敲成方孔问题*!



首先写下来功能要求(例如能够处理类型X的请求),非功能性要求(例如,每秒能够处理至少650个请求(如果您没有在要求中记下硬编号,那么您将不知道何时到停止优化!)),然后考虑你要处理的对象类型,需要相互通信的各个实体(客户端,服务器,代理等),然后考虑通信协议。



一旦你在那里,你就会更好地了解需要处理多少信息(以字节为单位),完成工作的时间以及是否所有实体实际上需要多线程。您似乎将此视为已授予或甚至是要求的一部分 - 但它纯粹是一种设计决策,而其他要求(例如交易安全)实际上可能与多线程的想法直接相反。 (并不是说它不能完成,只是它实施起来太难了,不值得你花时间)。



也许你会发现整体数据太少,你可以在单线程程序中轻松处理它。也许你会发现有太多东西要处理,你所拥有的硬件无法传递信息,更不用说处理它了。 (在这种情况下,是时候仔细检查要求,看看哪些部分可以降低到更实用的水平。)



简而言之,没有食谱书任何类型的并行编程,主要是因为它是一个复杂的领域,只有在需要专门解决方案的非常专业的应用程序中才需要。





*:事实上,现实世界的项目既不是正方形也不是圆形:它们更像是小行星或彗星 - 形状不规则,随着时间的推移而发出的许多要求造成的陨石坑斑点,并充满了稀有元素。但是一旦他们进入大气层,他们就会开始拆解成更多可管理的部分。这样做会更好,因为否则它们将不可避免地崩溃,让死去的程序员随之而来。
Not every problem is best solved with (existing) design patterns. You'd be surprised at the number of programs that were developed before the release of the Design Patterns book by the Go4.

That said, while multithreading wasn't as hot a topic at the time the aforementioned book was released as it is now, I'm sure many patterns can be reasonably applied to certain areas of a multithreaded application. However, just like the run-off-the-mill products you can cheaply buy everywhere are designed to be useful for the average user, design patterns are best used in a context with average requirements.

You have a strong requirement, i. e. implementing a high-performance system, and such requirements always need to be tailored to the specific problem and cicumstances! You may be able to adapt a design pattern to your requirements, but without knowing what, exactly, those requirements are, it's impossible to say if that is actually worth your while. Most likely, you'll be better off not wasting time on hammering a round-peg-pattern into a square-hole-problem*!

Start by writing down the functional requirements (e. g. able to handle requests of type X), non-functional requirements (e. g. able to handle at least 650 requests per second (if you don't write down a hard number in the requirements then you won't know when to stop optimizing!)), then consider the type of objects you deal with, the individual entities (client, server, proxy, whatever) that need to communicate with each other, and then consider communication protocols.

Once you're there, you'll have a better idea about how much information (in bytes) needs to be processed, how much time there is to do the job, and whether all of the entities actually need to be multithreaded at all. You seem to be taking this as granted or even part of the requirement - but it is purely a design decision, and other requirements (such as transaction safety) may in fact be directly opposed to the idea of multithreading. (not that it can't be done, just that it's so much harder to implement, it's not worth your time).

Maybe you'll find that there is so little data overall that you can easily process it in a single-threaded program. Maybe you'll find there is so much to process that the hardware you have is incapable to pass the information along, let alone process it. (In that case it's time to scrutinize the requirements and see which parts can be toned down to a more practical level.)

In short, there is no recipe book for parallel programming of any kind, mostly because it is a complex field, and only required in very specialized applications that require specialized solutions.


*: in truth, real world projects are neither square nor round: they're more like asteroids or comets - of irregular shape, speckled with craters caused by the many requirements fired at it over time, and filled with rare elements. But once they enter the atmosphere, they start disassembling into more managable bits. And that is for the better, because otherwise they would inevitably crash, leaving dead programmers in their wake.


我认为这个Stack Overflow问题可能让你感兴趣:TCP/\"UDP linux下的高性能服务器 [ ^ ]。
I think this Stack Overflow question may interest you: TCP/"UDP high-performance server under linux"[^].


这篇关于在多线程服务器应用程序中哪种设计模式更好?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆